title: ArrList 源码拜读
date: 2020-04-14 09:59:59
tags: [‘ArrList’,‘源码’,‘阅读’]
一般在阅读一个源码类的时候,首先先注意一下他的构造方法,十分重要
首先进入ArrList的无参构造方法,发现下面的代码
/**
* 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
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
可以看到这里的无参构造方法对elementData 赋值了,赋了一个空值,所以调用无参构造方法后arrayList的容量大小这个时候是0。
初始化完成后,我们可以开始看我们最常用的功能。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(size + 1);
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
这里在计算容量,如果当前数组为空,那么就应该启用默认大小,这里的默认大小是10
所以:arrList 使用无参构造方法,那么扩容的时候应该是在add()的时候进行扩容。
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);
}
这一段代码很重要,calculateCapacity()方法计算完容量之后就会来调用下面的ensureExplicitCapacity()方法
modCount++;这里暂时不清楚这个代表上面意思
下一行 如果当前传入的minCapacity大于当前数组的长度,那么就会干什么呢?
扩容 grow()
那么扩容具体是怎么操作的呢?
/**
* 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);
}
第一行代码 首先获取原有数组的长度
第二行是很多人不能理解的,这里主要是oldCapacity >> 1 晦涩难懂。
这里的大概意思是什么呢?
位运算,这里简单直接理解为老的除以2取整就行了
demo:
arrayList的扩容因子是1.5
15 >> 1 = 7 16 >> 1 = 8 19 >> 1 = 8
这里就是大家说的:arrayList的扩容因子是1.5
老的 + 0.5 = 1.5个老的
所以我们能得到结论,这里arrayList的扩容因子是1.5
arrayList 扩容判断
这里主要有一个比较存在疑惑
为什么newCapacity - MAX_ARRAY_SIZE > 0 而MAX_ARRAY_SIZE 竟然等于 Inter.MAX_VALUE = = 0x7fffffff;
但是这里MAX_ARRAY_SIZE = Inter.MAX_VALUE - 8;
这里我之前很不能理解为什么需要减8,后面了解到:
数组作为一个对象,需要一定的内存存储对象头信息,对象头信息最大占用内存不可超过8字节。
这里是为了除掉数组的内存存储对象头的信息
再后面就是生成一个新的数组,并且把原来的数组复制到新的数组里面,所以使用了Array.copyOf()方法
到这里ArrList就基本看完了。