Container-ArrayList bottom source code analysis (3)
-
The bottom layer of ArrayList is stored with an array
- Use Ctrl+right mouse button to click ArrayList to see the source code
List<String> a1=new ArrayList<>();//用Ctrl+鼠标右键 点击ArrayList看源码
-
Understand the inheritance structure and member variables of ArrayList, that is, initialize variables .
/** * Constructs an empty list with an initial capacity of ten. */ //ArrayList的构造器 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } //了解ArrayList的继承结构和成员变量 //ArrayList通过继承AbstractList父类实现 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { private static final long serialVersionUID = 8683452581122892189L; /** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10;//数组的默认长度为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 //elementData很重要的成员变量,为来我们放元素所操作的数组 //当无参构造器赋值以后,你就变成了一个空数组。 /** * 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 */ //看下它的无参构造 /** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } //先给elementData附的是一个空数组(节省内存)。
-
Add array elements
List<String> a1=new ArrayList<>(); a1.add("a");
-
Use Ctrl+right mouse button to click add to see the source code
boolean add(E e);
-
In Ctrl+Alt, some implementation classes of the add abstract method appear, click in the ArrayList to see the add method
/** * 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) { //这时的的size是0,ensureCapacityInternal就变成0+1=1 ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
-
Let's look at the method implemented by ensureCapacityInternal
//ensureCapacityInternal传进的参数是1,也就是minCapacity是1 private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); } //调用ensureExplicitCapacity方法,有嵌套调用了calculateCapacity(计算容量的方法) //这时calculateCapacity(elementData, minCapacity) //elementData 是一个空数组,minCapacity值是1
-
Then we continue to look at calculateCapacity (the method of calculating capacity)
private static int calculateCapacity(Object[] elementData, int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max(DEFAULT_CAPACITY, minCapacity); } //elementData 现在是一个空数组[],DEFAULTCAPACITY_EMPTY_ELEMENTDATA原来定义的也是一个空数组[],所以两者肯定是相等的。然后就会返回一个最大值,DEFAULT_CAPACITY定义的默认长度是10,minCapacity是原先传进来的值为1,所以最大值会返回10 return minCapacity; }
-
The value passed by the nested method of calculateCapacity is 10, which means that the method ensureExplicitCapacity passes 10.
//ensureCapacityInternal传进的参数是1,也就是minCapacity是1 private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); } //调用ensureExplicitCapacity方法,有嵌套调用了calculateCapacity(计算容量的方法) //这时calculateCapacity(elementData, minCapacity) //elementData 是一个空数组,minCapacity值是1
-
Then we look at the ensureExplicitCapacity method
private void ensureExplicitCapacity(int minCapacity) { //ensureExplicitCapacity传进来的参数是10,也就是minCapacity是10 modCount++;//改动的计时器,添加、删除...元素什么的 //这个非常重要,让我们判断是否要对数组进行扩容处理 // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } //现在minCapacity是10,elementData本来还是个空数组,现在的长度是0,所以条件是10-0=10大于0,满足条件,执行grow()这个核心的方法,数组的扩容。
-
Let's look at grow(minCapacity); the most important and core method of array expansion, ArryList
private void grow(int minCapacity) { //现在minCapacity是10 // overflow-conscious code int oldCapacity = elementData.length;//elementData.length是0,所以oldCapacity是0 int newCapacity = oldCapacity + (oldCapacity >> 1); //oldCapacity >> 1,右移一位就是除于2,但是0/2=0,还有加0,所以newCapacity是0 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; //newCapacity是0,minCapacity是10,newCapacity - minCapacity=-10,小于0满足条件,newCapacity = minCapacity=10,所以现在newCapacity是10 if (newCapacity - MAX_ARRAY_SIZE > 0)//这来是显然不成立的, MAX_ARRAY_SIZE=Interger-8,显然是一个比10大得很多的数,所以不进这个条件 newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); //这个才是最重要的,Arrays工具类的copyOf方法,elementData是空数组,newCapacity是10,于是现在elementData就变成了一个数组长度为10的数组,这也就完成了添加元素的方法 }
-
The grow(int minCapacity) is executed, that is, the ensureExplicitCapacity method is executed, that is, the ensureCapacityInternal is executed, that is, the final add must be executed.
public boolean add(E e) { //这时的的size是0,ensureCapacityInternal就变成0+1=1 ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e;//这时的elementData就是长度为10的数组,而现在size是0,++之后就变成了1,所以这样我们就完成了一个元素的添加 return true; }
-
-
When we add the second element, will it expand? Answer: No
-
Add element
public boolean add(E e) { //这时的的size是0,ensureCapacityInternal就变成0+1=1 ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e;//这时的elementData就是长度为10的数组,而现在size是0,++之后就变成了1,所以这样我们就完成了一个元素的添加 return true; }
-
densureCapacityInternal passed 2
private void ensureCapacityInternal(int minCapacity) { //2 ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); }
-
calculateCapacity returns 2
private static int calculateCapacity(Object[] elementData, int minCapacity) { //elementData是10 ,minCapacity是2 if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { //DEFAULTCAPACITY_EMPTY_ELEMENTDATA还是空数组,elementData已经是10显然不相等了 return Math.max(DEFAULT_CAPACITY, minCapacity); } return minCapacity;//就会直接返回2 }
-
The return of ensureExplicitCapacity is 2. Next we look at the ensureExplicitCapacity method
private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); } //ensureExplicitCapacity这时返回的是2
-
ensureExplicitCapacity is 2, that is, minCapacity is 2, minCapacity-elementData.length=2-10=-8 is not greater than 0, so there will be no further expansion, so if minCapacity is between 1-10, there will be no expansion, knowing that minCapacity is 11. , Will continue to expand
private void ensureExplicitCapacity(int minCapacity) { //ensureExplicitCapacity是2,也就是 minCapacity是2 modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0)//2-10=-8不大于0,所以不会再扩容 grow(minCapacity); }
-
-
Assuming that the array has been added to 11 , it will be expanded by 1.5 times. That is, minCapacity is 11, and finally expanded into an array of length 15
-
That is, minCapacity is 11
private void ensureExplicitCapacity(int minCapacity) { //ensureExplicitCapacity是2,也就是 minCapacity是2 modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0)//11-10=1大于0,就会扩容 grow(minCapacity); }
-
Let's look at the expansion method grow(minCapacity);
private void grow(int minCapacity) { //minCapacity=11 // overflow-conscious code int oldCapacity = elementData.length; // oldCapacity = elementData.length;=10,所以 oldCapacity是10 int newCapacity = oldCapacity + (oldCapacity >> 1); //newCapacity = oldCapacity + (oldCapacity >> 1)=10+10/2=15,所以newCapacity是15 if (newCapacity - minCapacity < 0)//15-10=5大于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); //于是最关键的一步,elementData是10,newCapacity是15,最后elementData变成了一个数组长度为15的数组,又扩容了。 }
-