Java7集合框架——ArrayList

Posted wpbxin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java7集合框架——ArrayList相关的知识,希望对你有一定的参考价值。

 java.util.ArrayList

ArrayList的内部实现

ArrayList是一个内部以数组方式实现列表、可以自动扩容的集合。其内部实现有4个重要的变量:

  1. DEFAULT_CAPACITY:静态变量,是默认的初始化元素个数(容量)。
  2. EMPTY_ELEMENTDATA:静态变量,所有对象实例共享,是默认的空实现,所有调用空构造器public ArrayList()构造的ArrayLis默认指向该变量。
  3. elementData是常规的数组,存储列表的元素。
  4. sizeelementData数组中实际添加操作add占用的元素个数

源码如下:

 1    /**
 2      * Default initial capacity.
 3      */
 4     private static final int DEFAULT_CAPACITY = 10;
 5 
 6     /**
 7      * Shared empty array instance used for empty instances.
 8      */
 9     private static final Object[] EMPTY_ELEMENTDATA = {};
10 
11     /**
12      * The array buffer into which the elements of the ArrayList are stored.
13      * The capacity of the ArrayList is the length of this array buffer. Any
14      * empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
15      * DEFAULT_CAPACITY when the first element is added.
16      */
17     private transient Object[] elementData;
18 
19     /**
20      * The size of the ArrayList (the number of elements it contains).
21      *
22      * @serial
23      */
24     private int size;

 ArrayList添加元素和扩容

源码如下,

 1    /**
 2      * Appends the specified element to the end of this list.
 3      *
 4      * @param e element to be appended to this list
 5      * @return <tt>true</tt> (as specified by {@link Collection#add})
 6      */
 7     public boolean add(E e) {
 8         ensureCapacityInternal(size + 1);  // Increments modCount!!
 9         elementData[size++] = e;
10         return true;
11     }

 

add方法调用的了ensureCapacityInternal(size + 1),首先是判断元素的容量(capacity)大小,如果小于默认的DEFAULT_CAPACITY = 10,则使用默认容量,可以看出,只要添加了数组元素,数组的最小容量就是10。而ensureCapacityInternal(size + 1)调用了ensureExplicitCapacity(minCapacity),这个方法记录了总共需要扩容的次数modCount,同时进行了实际的数组扩容。当然只有实际数组长度size超过时才会进行扩容。

 1    private void ensureCapacityInternal(int minCapacity) {
 2         if (elementData == EMPTY_ELEMENTDATA) {
 3             minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
 4         }
 5 
 6         ensureExplicitCapacity(minCapacity);
 7     }
 8 
 9     private void ensureExplicitCapacity(int minCapacity) {
10         modCount++;
11 
12         // overflow-conscious code
13         if (minCapacity - elementData.length > 0)
14             grow(minCapacity);
15     }

 

扩容的方法如下。对于没有溢出的,实际的扩容是增加了一半容量,这里使用了位运算,右移一位类似与/2操作,取了一半,但是位操作快。

这里需要说明的是,默认空构造器时建立的ArrayList也是在这里首次进行扩容的,使用默认容量10。

 1     /**
 2      * Increases the capacity to ensure that it can hold at least the
 3      * number of elements specified by the minimum capacity argument.
 4      *
 5      * @param minCapacity the desired minimum capacity
 6      */
 7     private void grow(int minCapacity) {
 8         // overflow-conscious code
 9         int oldCapacity = elementData.length;
10         int newCapacity = oldCapacity + (oldCapacity >> 1);
11         if (newCapacity - minCapacity < 0)
12             newCapacity = minCapacity;
13         if (newCapacity - MAX_ARRAY_SIZE > 0)
14             newCapacity = hugeCapacity(minCapacity);
15         // minCapacity is usually close to size, so this is a win:
16         elementData = Arrays.copyOf(elementData, newCapacity);
17     }
18 
19     private static int hugeCapacity(int minCapacity) {
20         if (minCapacity < 0) // overflow
21             throw new OutOfMemoryError();
22         return (minCapacity > MAX_ARRAY_SIZE) ?
23             Integer.MAX_VALUE :
24             MAX_ARRAY_SIZE;
25     }

 

elementData[size++] = e;就是直接的元素赋值,然后增加size。另外还有指定索引添加元素,代码如下。

  1. 判断越界
  2. 扩容
  3. 从指定索引处的元素往后移动一位
  4. 插入指定索引的指定元素,完成。
 1     /**
 2      * Inserts the specified element at the specified position in this
 3      * list. Shifts the element currently at that position (if any) and
 4      * any subsequent elements to the right (adds one to their indices).
 5      *
 6      * @param index index at which the specified element is to be inserted
 7      * @param element element to be inserted
 8      * @throws IndexOutOfBoundsException {@inheritDoc}
 9      */
10     public void add(int index, E element) {
11         rangeCheckForAdd(index);
12 
13         ensureCapacityInternal(size + 1);  // Increments modCount!!
14         System.arraycopy(elementData, index, elementData, index + 1,
15                          size - index);
16         elementData[index] = element;
17         size++;
18     }

 

ArrayList删除元素

删除元素有2种:

  1. 指定索引删除元素
  2. 指定元素删除:这里找到第一个equals或者null(如元素为null)即可

1. 指定索引删除元素

  1. 越界判断
  2. 修改总共需要扩容的次数modCount
  3. 记录指定索引的旧元素
  4. 非最后一个元素,即index != size - index - 1,把元素前移一个单位。
  5. 清空最后一个索引元素
  6. 返回旧元素
 1     /**
 2      * Removes the element at the specified position in this list.
 3      * Shifts any subsequent elements to the left (subtracts one from their
 4      * indices).
 5      *
 6      * @param index the index of the element to be removed
 7      * @return the element that was removed from the list
 8      * @throws IndexOutOfBoundsException {@inheritDoc}
 9      */
10     public E remove(int index) {
11         rangeCheck(index);
12 
13         modCount++;
14         E oldValue = elementData(index);
15 
16         int numMoved = size - index - 1;
17         if (numMoved > 0)
18             System.arraycopy(elementData, index+1, elementData, index,
19                              numMoved);
20         elementData[--size] = null; // clear to let GC do its work
21 
22         return oldValue;
23     }

 

2. 指定元素删除

删除的步骤类似,只是这里是遍历找到第一个指定的元素而已

 1     /**
 2      * Removes the first occurrence of the specified element from this list,
 3      * if it is present.  If the list does not contain the element, it is
 4      * unchanged.  More formally, removes the element with the lowest index
 5      * <tt>i</tt> such that
 6      * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
 7      * (if such an element exists).  Returns <tt>true</tt> if this list
 8      * contained the specified element (or equivalently, if this list
 9      * changed as a result of the call).
10      *
11      * @param o element to be removed from this list, if present
12      * @return <tt>true</tt> if this list contained the specified element
13      */
14     public boolean remove(Object o) {
15         if (o == null) {
16             for (int index = 0; index < size; index++)
17                 if (elementData[index] == null) {
18                     fastRemove(index);
19                     return true;
20                 }
21         } else {
22             for (int index = 0; index < size; index++)
23                 if (o.equals(elementData[index])) {
24                     fastRemove(index);
25                     return true;
26                 }
27         }
28         return false;
29     }
30 
31     /*
32      * Private remove method that skips bounds checking and does not
33      * return the value removed.
34      */
35     private void fastRemove(int index) {
36         modCount++;
37         int numMoved = size - index - 1;
38         if (numMoved > 0)
39             System.arraycopy(elementData, index+1, elementData, index,
40                              numMoved);
41         elementData[--size] = null; // clear to let GC do its work
42     }

 

ArrayList查找元素

这里只做了越界检查,如果没有越界,直接返回指定索引的元素即可。

 1     /**
 2      * Returns the element at the specified position in this list.
 3      *
 4      * @param  index index of the element to return
 5      * @return the element at the specified position in this list
 6      * @throws IndexOutOfBoundsException {@inheritDoc}
 7      */
 8     public E get(int index) {
 9         rangeCheck(index);
10 
11         return elementData(index);
12     }

 

ArrayList修改元素

指定索引修改元素

 1     /**
 2      * Replaces the element at the specified position in this list with
 3      * the specified element.
 4      *
 5      * @param index index of the element to replace
 6      * @param element element to be stored at the specified position
 7      * @return the element previously at the specified position
 8      * @throws IndexOutOfBoundsException {@inheritDoc}
 9      */
10     public E set(int index, E element) {
11         rangeCheck(index);
12 
13         E oldValue = elementData(index);
14         elementData[index] = element;
15         return oldValue;
16     }

 

ArrayList的特点

  1. 允许空的元素
  2. 允许重复的元素
  3. 元素有序:读取数据和存放数据的顺序一致
  4. 线程不安全
  5. 随机访问指定索引的元素快,顺序添加元素快
  6. 删除和插入元素慢,因涉及到复制和移动其他的元素

ArrayList的元素实现elementData使用了transient

ArrayList本身实现了CloneableSerializable,但是关键的成员变量却是用了transient进行了修饰,不希望被序列化。

1 public class ArrayList<E> extends AbstractList<E>
2         implements List<E>, RandomAccess, Cloneable, java.io.Serializable

 

从序列化的实现来看,只对elementData中有元素的部分进行了序列化,并没有全部元素,这是合理的,一般elementData的容量比实际的size大,没有必要所有元素都行序列化。这也提高了时间效率,同时节省了空间。

 1     /**
 2      * Save the state of the <tt>ArrayList</tt> instance to a stream (that
 3      * is, serialize it).
 4      *
 5      * @serialData The length of the array backing the <tt>ArrayList</tt>
 6      *             instance is emitted (int), followed by all of its elements
 7      *             (each an <tt>Object</tt>) in the proper order.
 8      */
 9     private void writeObject(java.io.ObjectOutputStream s)
10         throws java.io.IOException{
11         // Write out element count, and any hidden stuff
12         int expectedModCount = modCount;
13         s.defaultWriteObject();
14 
15         // Write out size as capacity for behavioural compatibility with clone()
16         s.writeInt(size);
17 
18         // Write out all elements in the proper order.
19         for (int i=0; i<size; i++) {
20             s.writeObject(elementData[i]);
21         }
22 
23         if (modCount != expectedModCount) {
24             throw new ConcurrentModificationException();
25         }
26     }

 

以上是关于Java7集合框架——ArrayList的主要内容,如果未能解决你的问题,请参考以下文章

Java常见集合的默认大小及扩容机制

Java 集合学习笔记:ArrayList

Java 集合学习笔记:ArrayList

Java 集合学习笔记:ArrayList

Java7/8集合框架——HashMap

Java自学-集合框架 ArrayList和HashSet的区别