码迷,mamicode.com
首页 > 编程语言 > 详细

java集合类源码分析之List(一)

时间:2017-10-06 19:04:46      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:指定位置   语言   view   区别   data   its   contain   splay   转换   

  首先分析一下集合与数组的区别:1.java中的数组一般用于存储基本数据类型,而且是静态的,即长度固定不变,这就不适用于元素个数未知的情况;2.集合只能用于存储引用类型,并且长度可变,适用于大多数情况,可用toArray()方法转换成数组。

java语言提供了多种集合类的接口,如List、Set、Map等。这里首先从List开始分析,其实现类有ArrayList、LinkedList、Vector(Stack),下面就从它们的源代码开始:

1.ArrayList(数组列表)

顾名思义,这是一种以数组形式进行存储的列表,所以其优点是便于随机访问,而在插入和删除操作时效率较低

  • 构造方法(三种):

1.public ArrayList(int initialCapacity)  指定初始列表的容量,当容量不够时会自动进行扩容

2.public ArrayList()  空的构造方法

3.public ArrayList(Collection<? extends E> c)  初始化列表元素,传入参数为一个集合类的对象

技术分享
1         ArrayList<String> arrayList1 = new ArrayList<String>(3);    //初始化ArrayList容量大小
2         arrayList1.add("A");
3         arrayList1.add("B");
4         arrayList1.add("C");
5         ArrayList<String> arrayList2 = new ArrayList<String>(arrayList1);    //初始化ArrayList元素
6         System.out.println(arrayList2);    //[A, B, C]
View Code
  • 插入元素(add)

1.在列表尾部插入 add(E e)

技术分享
1  public boolean add(E e) {
2         ensureCapacityInternal(size + 1);  // Increments modCount!!
3         elementData[size++] = e;
4         return true;
5     }
View Code

2.在列表指定位置插入 add(int index, E element)

技术分享
1 public void add(int index, E element) {
2         rangeCheckForAdd(index);
3 
4         ensureCapacityInternal(size + 1);  // Increments modCount!!
5         System.arraycopy(elementData, index, elementData, index + 1,
6                          size - index);
7         elementData[index] = element;
8         size++;
9     }
View Code

3.在列表尾部插入一个子集 addAll(Collection<? extends E> c)

技术分享
1     public boolean addAll(Collection<? extends E> c) {
2         Object[] a = c.toArray();
3         int numNew = a.length;
4         ensureCapacityInternal(size + numNew);  // Increments modCount
5         System.arraycopy(a, 0, elementData, size, numNew);
6         size += numNew;
7         return numNew != 0;
8     }
View Code

4.在列表的指定位置插入一个子集 addAll(int index, Collection<? extends E> c)

技术分享
 1     public boolean addAll(int index, Collection<? extends E> c) {
 2         rangeCheckForAdd(index);
 3 
 4         Object[] a = c.toArray();
 5         int numNew = a.length;
 6         ensureCapacityInternal(size + numNew);  // Increments modCount
 7 
 8         int numMoved = size - index;
 9         if (numMoved > 0)
10             System.arraycopy(elementData, index, elementData, index + numNew,
11                              numMoved);
12 
13         System.arraycopy(a, 0, elementData, index, numNew);
14         size += numNew;
15         return numNew != 0;
16     }
View Code

观察源码可知,在指定位置插入元素其实是通过arraycopy()方法来实现的,即是将原数组复制到目标数组,因而效率较低

应用示例:

技术分享
1         ArrayList<String> arrayList1 = new ArrayList<String>(3);    //初始化ArrayList容量大小
2         arrayList1.add("A");
3         arrayList1.add("B");
4         arrayList1.add("C");
5         arrayList1.add("D");
6         System.out.println(arrayList1);    //[A, B, C, D]
7         arrayList1.add(0, "E");
8         System.out.println(arrayList1); //[E, A, B, C, D]
View Code
  • 查找元素

1.查找指定位置的元素 get(int index)

2.查找指定元素的位置 indexOf(Object o)、lastIndexOf(Object o)

3.查找列表中是否包含指定元素 contains(Object o)

技术分享
1  public boolean contains(Object o) {
2         return indexOf(o) >= 0;
3     }
View Code

由于ArrayList以数组方式实现,自带索引,所以便于随机查找

  • 修改元素

修改指定位置的元素 set(int index, E element)

技术分享
1         ArrayList<String> arrayList1 = new ArrayList<String>(3);    //初始化ArrayList容量大小
2         arrayList1.add("A");
3         arrayList1.add("B");
4         arrayList1.add("C");
5         arrayList1.add("D");
6         System.out.println(arrayList1); //[A, B, C, D]
7         arrayList1.set(0, "X");
8         System.out.println(arrayList1); //[X, B, C, D]
View Code

由于ArrayList以数组方式实现,自带索引,所以便于随机修改

  • 删除元素

1.删除指定位置的元素 remove(int index)

技术分享
 1     public E remove(int index) {
 2         rangeCheck(index);
 3 
 4         modCount++;
 5         E oldValue = elementData(index);
 6 
 7         int numMoved = size - index - 1;
 8         if (numMoved > 0)
 9             System.arraycopy(elementData, index+1, elementData, index,
10                              numMoved);
11         elementData[--size] = null; // clear to let GC do its work
12 
13         return oldValue;
14     }
View Code

2.删除指定元素 remove(Object o)

技术分享
 1     public boolean remove(Object o) {
 2         if (o == null) {
 3             for (int index = 0; index < size; index++)
 4                 if (elementData[index] == null) {
 5                     fastRemove(index);
 6                     return true;
 7                 }
 8         } else {
 9             for (int index = 0; index < size; index++)
10                 if (o.equals(elementData[index])) {
11                     fastRemove(index);
12                     return true;
13                 }
14         }
15         return false;
16     }
View Code

3.清空列表 clear()

技术分享
1     public void clear() {
2         modCount++;
3 
4         // clear to let GC do its work
5         for (int i = 0; i < size; i++)
6             elementData[i] = null;
7 
8         size = 0;
9     }
View Code

从源码中我们可以知道,删除元素时也是通过arraycopy()方法,将原数组复制到目标数组,因而效率较低

应用示例:

技术分享
 1         ArrayList<String> arrayList1 = new ArrayList<String>(3);    //初始化ArrayList容量大小
 2         arrayList1.add("A");
 3         arrayList1.add("B");
 4         arrayList1.add("C");
 5         arrayList1.add("D");
 6         arrayList1.remove(0);
 7         System.out.println(arrayList1);    //[B, C, D]
 8         arrayList1.remove("D");
 9         System.out.println(arrayList1);    //[B, C]
10         arrayList1.clear();
11         System.out.println(arrayList1.isEmpty());    //true
View Code

 

java集合类源码分析之List(一)

标签:指定位置   语言   view   区别   data   its   contain   splay   转换   

原文地址:http://www.cnblogs.com/Wilange/p/7631933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!