一、ArrayList类图
二、ArrayList主要的成员变量
(1)private static final int DEFAULT_CAPACITY = 10;默认容量。
(2)private static final Object[] EMPTY_ELEMENTDATA = {};
Shared empty array instance used for empty instances.
(3)private static final Object[] DEFAULTCAPACITY_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.
(4)transient Object[] elementData;
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.
(5)private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
The maximum size of array to allocate. Some VMs reserve some header words in an array. Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit
三、构造函数
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);
}
}
调用带参数的构造函数,当initialCapacity>0时直接new Object[initialCapacity]的数组,如果initialCapacity=0,则赋值为EMPTY_ELEMENTDATA
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
调用无参构造函数时,设置this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA,当加入一个元素第一次扩容时会扩容为DEFAULT_CAPACITY
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;
}
}
// c.toArray might (incorrectly) not return Object[] (see 6260652)解释:https://blog.csdn.net/xiaotuji_/article/details/72598546
三、add方法及扩容
1、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;
}
扩容逻辑在ensureCapacityInternal(int minCapacity)里面。ensureCapacityInternal的代码逻辑如下:
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
calculateCapacity里面有涉及到DEFAULTCAPACITY_EMPTY_ELEMENTDATA的处理,代码如下:
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
处理完DEFAULTCAPACITY_EMPTY_ELEMENTDATA之后接着执行,ensureExplicitCapacity,如下:
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
如果minCapacity > elementData.length,执行grow(minCapacity)进行扩容,如下:
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
仅记录个人学习情况,如有错误,请帮指正,谢谢!