/**
* 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); // 进行扩容校验
elementdata[size++] = e; // 将值添加到数组后面,并将 size+1
return true;
}
/**
* 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
private void ensurecapacityinternal(int mincapacity) {
ensureexplicitcapacity(calculatecapacity(elementdata, mincapacity)); // elementdata 数组
}
/**
* default initial capacity.
*/
private static final int default_capacity = 10;
/**
* 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 = {};
// 返回最大的 index
private static int calculatecapacity(object[] elementdata, int mincapacity) {
if (elementdata == defaultcapacity_empty_elementdata) { // 与空数组实例对比
return math.max(default_capacity, mincapacity);
}
return mincapacity;
}
private void ensureexplicitcapacity(int mincapacity) {
modcount++;
// overflow-conscious code
if (mincapacity - elementdata.length > 0)
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);
}
add(int index, e element) 源码分析
/**
* inserts the specified element at the specified position in this
* list. shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws indexoutofboundsexception {@inheritdoc}
*/
public void add(int index, e element) {
rangecheckforadd(index); // 校验index是否超过当前定义的数组大小范围,超过则抛出 indexoutofboundsexception
ensurecapacityinternal(size + 1); // increments modcount!!
system.arraycopy(elementdata, index, elementdata, index + 1,
size - index); // 复制,向后移动
elementdata[index] = element;
size++;
}
/**
* a version of rangecheck used by add and addall.
*/
private void rangecheckforadd(int index) {
if (index > size || index < 0)
throw new indexoutofboundsexception(outofboundsmsg(index));
}