八股文 – ArrayList
官方说明:List接口的容量可变数组实现。实现所有父类的的列表操作,并允许所有添加null以外的所有元素。除了实现List接口外,这个类还提供了一些方法来操作用于内部存储列表的数组的大小。
源码简析
- 属性部分
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable,
java.io.Serializable
ArrayList 继承自 AbstractList,实现了 List、RandomAccess、Cloneable、Serializable 接口。分别对应了基础的List属性(基于数组的实现)、可快速访问、可复制、可序列化的属性
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
Size是指ArrayList中实际有多少数据。
elementData对象数组是容纳arrayList对象的缓冲区,初始为{},在加入任何元素之后将初始化为默认初始容量 DEFAULT_CAPACITY (10)。
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
这里做了一个特殊操作,有初始容量的构造器用的初始化对象和完全空入参的构造器用的初始化对象虽然都是 {} 但是是被区分开来的,ArrayList官方给的说明是方便确认如何进行扩容,区分开后各自的使用和扩容的确认我们暂且不管,继续看。
- 构造器部分
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
空参构造:纯净的空参构造赋值 DEFAULTCAPACITY_EMPTY_ELEMENTDATA ,并在第一次赋值的时候初始化变量10。
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
数量构造:只赋值初始容量,如果初始容量是0,那么赋值 EMPTY_ELEMENTDATA,如果小于0那么就抛异常。
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
集合构造:通过集合类进行构造:
- 如果用于初始化的集合类的size为空(没有任何一个对象元素在里面),那么就和数量为0的数量构造相同;
-
如果初始化集合类不为空,先转换为Array对象(
toArray()
在ArrayList中的实现为Arrays.copyOf(elementData, size)
),然后由于集合类的toArray()
方法不一定返回的都是Object[]
对象,所以需要进行额外的判断。(比如<T> T[] toArray(T[] a)
方法会返回泛型数据而非对象数组),如果返回的不是对象数组,则需要进行转换赋值。
-
操作函数部分
-
Add
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
暂无评论