1.List:ArrayList和LinkedList 都是有序、可重复、有索引。但是这两个底层采用的数据结构(存储、组织数据的方式)不同,适用场景不同。
List集合支持索引,有很多与索引相关的方法。
2.List支持的遍历方法
(1)for,因为有索引
(2)迭代器
(3)增强for
(4)Lambda表达式
3.ArrayList集合的底层原理
ArrayList
支持使用索引进行随机访问,时间复杂度为 O(1),这使得其在访问元素时非常高效。- 在末尾添加元素的时间复杂度平均为 O(1)(当没有发生扩容时)。
- 在中间或开始位置插入或删除元素时,时间复杂度为 O(n),因为需要移动数组中的元素。
ArrayList集合适用:根据索引查询数据,比如根据随即索引取数据,或者数据量不大
不适用:数据量大同时需要频繁增删。
4.LinkedList集合的底层原理
基于双链表实现
双链表
5.LinkedList集合的应用场景
(1)设计队列,先进先出,后进后出。
(2)设计栈,先进后出push/pop
上面可以改成push/pop操作