Detailed explanation of the expansion mechanism of the collection class ArrayList

The construction method of the ArrayList class is overloaded, providing two construction methods of no-argument construction and construction with parameters. An elementData array is maintained inside the ArrayList class to store the added objects. (About the transient keyword: the attribute modified by this keyword will not be serialized or serialized).

Table of contents

No parameter construction expansion mechanism

Expansion mechanism with parameter construction

 Summarize


When the storage capacity of the array is not enough to store data, there are the following mechanisms for expanding the array:

  • The expansion mechanism of the ArrayList object created by using the no-argument construction method
    The size of the initialized elementData array is 0;
    when adding the first object to elementData, the size of the element will be expanded to 10;
    when the capacity is insufficient and needs to be expanded again, the capacity Expanded to 1.5 times the original.
  • The expansion mechanism of the ArrayList object created by the parameterized construction method.
    The size of the initialized elementData array is the size of the parameter
    . When expansion is required, the consignment expansion of elementData is 1.5 times the original

No parameter construction expansion mechanism

Use the following program segment to trace the source code to understand the expansion mechanism of ArrayList's no-argument construction instantiated object:

@SuppressWarnings({"all"})
public class ArrayListTestDrive {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();//使用了无参构造器
        //实例化结束后arrayList内部维护的elementData的大爱小为0
        for(int i=1;i<=10;i++) {
            arrayList.add(i);//首次执行add,对elementData数组扩容,大小为10
        }
        //进行扩容,size为10+10/2 = 15
        for(int i=11;i<=15;i++) {
            arrayList.add(i);
        }
        arrayList.add(100);//再次扩容,size为15 + 15/2 = 22
        arrayList.add(200);
    }
}


Use the following program segment to trace the source code to understand the expansion mechanism of ArrayList's parameterized construction instantiation object:

Expansion mechanism with parameter construction

@SuppressWarnings({"all"})
public class ArrayListTestDrive {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>(8);//使用了有参构造器,初始化elementData数组的大小为8
        for(int i=1;i<=8;i++) {
            arrayList.add(i);
        }
        //进行扩容,扩容后的大小为12
        for(int i=9;i<=12;i++) {
            arrayList.add(i);
        }
        arrayList.add(100);//继续扩容,大小将为18
        arrayList.add(200);
    }
}

 Summarize

  • When using the parameterless constructor, the size of the elementData array maintained in the instantiated object of ArrayList is 0 by default. When the object is added for the first time, the size of the elementData array is set to 10, and then the size of the elementData array is set to 1.5 times the original size when the capacity is increased again.
  • Using the constructor with parameters, the size of the elementData array maintained in the instantiation object of ArrayList is initialized to the size specified by the parameters. When the server expands again, the size of the elementData array is always set to 1.5 times the original size.

Guess you like

Origin blog.csdn.net/weixin_64450588/article/details/127968223