Collection子接口之一:List接口 List接口概述:
鉴于java中数组用来存储数据的局限性,我们通常使用List替代数组。
List集合类中元素有序、且可重复,集合中的每个元素都有其对应的顺序索引。
List容器中的元素对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素。
JDK API中List接口的实现类常用的有:ArrayList、LinkedList和Vector。
List接口框架 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |----Collection接口:单列集合,用来存储一个一个的对象 |----List接口:存储有序的、可重复的数据。 -->“动态”数组,替换原有的数组 |----ArrayList:作为List接口的主要实现类;线程不安全的,效率高;底层使用Object[] elementData存储 |----LinkedList:对于频繁的插入、删除操作,使用此类效率比ArrayList高;底层使用双向链表存储 |----Vector:作为List接口的古老实现类;线程安全的,效率低;底层使用Object[] elementData存储 ------------------------------------------------------------------------------------------------- 2. ArrayList的源码分析:2.1 jdk 7 情况下:ArrayList list = new ArrayList(); list.add(123 ); ... list.add(11 ); 默认情况下,扩容为原来的容量的1.5 倍,同时需要将原有数组中的数据复制到新的数组中。 * 结论:建议开发中使用带参的构造器:ArrayList list = new ArrayList(int capacity) -------------------------------------------------------------------------------------------------- 2.2 jdk 8 中ArrayList的变化:ArrayList list = new ArrayList(); * list.add(123 ); ... 后续的添加和扩容操作与jdk 7 无异。 2.3 小结:jdk7中的ArrayList的对象的创建类似于单例的饿汉式,而jdk8中的ArrayList的对象的创建类似于单例的懒汉式,延迟了数组的创建,节省内存。 ---------------------------------------------------------------------------------------------------- 3. LinkedList的源码分析:LinkedList list = new LinkedList(); 内部声明了Node类型的first和last属性,默认值为null list.add(123 ); 其中,Node定义为:体现了LinkedList的双向链表的说法 private static class Node <E > { E item; Node<E> next; Node<E> prev; ``` Node(Node<E> prev, E element, Node<E> next) { this .item = element; this .next = next; this .prev = prev; } ``` } ---------------------------------------------------------------------------------------------------- 4. Vector的源码分析:jdk7和jdk8中通过Vector()构造器创建对象时,底层都创建了长度为10 的数组。在扩容方面,默认扩容为原来的数组长度的2 倍。 面试题:ArrayList、LinkedList、Vector三者的异同? 相同:三个类都是实现了List接口,存储数据的特点相同:存储有序的、可重复的数据。 不同:请阅读上文。
List接口中常用方法:
void add(int index, Object ele):在index位置插入ele元素
boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来
Object get(int index):获取指定index位置的元素
int indexOf(Object obj):返回obj在集合中首次出现的位置
int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
Object remove(int index):移除指定index位置的元素,并返回此元素
Object set(int index, Object ele):设置指定index位置的元素为ele
List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 public class ListTest { public void test2 () { ArrayList list = new ArrayList(); list.add(123 ); list.add(456 ); list.add("AA" ); list.add(new Person("Tom" ,12 )); list.add(456 ); int index = list.indexOf(4567 ); System.out.println(index); System.out.println(list.lastIndexOf(456 )); Object obj = list.remove(0 ); System.out.println(obj); System.out.println(list); list.set(1 ,"CC" ); System.out.println(list); List subList = list.subList(2 , 4 ); System.out.println(subList); System.out.println(list); } @Test public void test1 () { ArrayList list = new ArrayList(); list.add(123 ); list.add(456 ); list.add("AA" ); list.add(new Person("Tom" ,12 )); list.add(456 ); System.out.println(list); list.add(1 ,"BB" ); System.out.println(list); List list1 = Arrays.asList(1 , 2 , 3 ); list.addAll(list1); System.out.println(list.size()); System.out.println(list.get(0 )); } } 总结:常用方法 增:add(Object obj) 删:remove(int index) / remove(Object obj) 改:set(int index, Object ele) 查:get(int index) 插:add(int index, Object ele) 长度:size()
List遍历:
Iterator迭代器方式
增强for循环
普通的循环
@Test
public void test3(){
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("AA");
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Iterator iterator = list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } System.out.println("***************" ); for (Object obj : list){ System.out.println(obj); } System.out.println("***************" ); for (int i = 0 ;i < list.size();i++){ System.out.println(list.get(i)); }
}