代码ArrayList源码学习jdk17
目录
【代码】ArrayList源码学习(jdk17)
源码解析
成员变量
/**
* Default initial capacity.
*/
// 数组初始容量
private static final int 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 = {};
/**
* 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;
构造方法
/**
* 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);
}
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* 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) {
Object[] a = c.toArray();
if ((size = a.length) != 0) {
if (c.getClass() == ArrayList.class) {
elementData = a;
} else {
// 不是ArrayList类型就进行拷贝
elementData = Arrays.copyOf(a, size, Object[].class);
}
} else {
// replace with empty array.
elementData = EMPTY_ELEMENTDATA;
}
}
添加元素
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
modCount++;
add(e, elementData, size);
return true;
}
/**
* This helper method split out from add(E) to keep method
* bytecode size under 35 (the -XX:MaxInlineSize default value),
* which helps when add(E) is called in a C1-compiled loop.
*/
private void add(E e, Object[] elementData, int s) {
// 如果size s和数组长度一样需要扩容
if (s == elementData.length)
elementData = grow();
// 扩容完后把值放到数组里
elementData[s] = e;
// 数组规模+1
size = s + 1;
}
private Object[] grow() {
// 扩容的最小容量是当前容量+1
return grow(size + 1);
}
/**
* 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
* @throws OutOfMemoryError if minCapacity is less than zero
*/
private Object[] grow(int minCapacity) {
// 获取了数组的当前容量
int oldCapacity = elementData.length;
// 判断当前的容量是否大于0,或者数组是否不等于默认的空数组。如果是,那么就需要增加数组的容量
if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
// 计算新容量,传入老容量,需要增加的最小容量,老容量的0.5倍
int newCapacity = ArraysSupport.newLength(oldCapacity,
minCapacity - oldCapacity, /* minimum growth */
oldCapacity >> 1 /* preferred growth */);
// 使用Arrays.copyOf方法来创建一个新的数组,这个新的数组的容量就是刚刚计算出来的新的容量。然后,代码将原数组的所有元素复制到新的数组中
return elementData = Arrays.copyOf(elementData, newCapacity);
} else {
// 否则,直接创建一个新的数组,这个新的数组的容量是默认的容量10和需要的最小容量中的较大值
return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];
}
}
// jdk17计算容量的方法
public static int newLength(int oldLength, int minGrowth, int prefGrowth) {
// preconditions not checked because of inlining
// assert oldLength >= 0
// assert minGrowth > 0
// 新长度是加上增长量和旧容量1.5倍的较大值
int prefLength = oldLength + Math.max(minGrowth, prefGrowth); // might overflow
// 如果新长度没有整数溢出,且小于SOFT_MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8,就返回,这个arraylist的最大值
if (0 < prefLength && prefLength <= SOFT_MAX_ARRAY_LENGTH) {
return prefLength;
} else {
// 如果长度超长了,传入旧长度和最小增长量
// put code cold in a separate method
return hugeLength(oldLength, minGrowth);
}
}
private static int hugeLength(int oldLength, int minGrowth) {
// 计算最小扩容长度
int minLength = oldLength + minGrowth;
//太大,整数溢出了就抛异常
if (minLength < 0) { // overflow
throw new OutOfMemoryError(
"Required array length " + oldLength + " + " + minGrowth + " is too large");
// 如果小于arraylist最大长度就用这个最大长度
} else if (minLength <= SOFT_MAX_ARRAY_LENGTH) {
return SOFT_MAX_ARRAY_LENGTH;
} else {
// 否则用这个最小长度,也就是大于SOFT_MAX_ARRAY_LENGTH小于int最大值
return minLength;
}
}