ArrayList: how does the size increase?

后端 未结 19 759
既然无缘
既然无缘 2020-11-29 18:11

I have a basic question on Java ArrayList.

When ArrayList is declared and initialized using the default constructor, memory space for 10 el

相关标签:
19条回答
  • 2020-11-29 18:26

    Context java 8

    I give my answer here in the context of Oracle java 8 implementation, since after reading all the answers, I found that an answer in the context of java 6 has given by gmgmiller, and another answer has been given in the context of java 7. But how java 8 implementes the size increasement has not been given.

    In java 8, the size increasement behavior is the same as java 6, see the grow method of ArrayList:

       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);
        }
    

    the key code is this line:

    int newCapacity = oldCapacity + (oldCapacity >> 1);
    

    So clearly, the growth factor is also 1.5, the same as java 6.

    0 讨论(0)
  • 2020-11-29 18:26

    ArrayList does increases the size on load factor on following cases:

    • Initial Capacity: 10
    • Load Factor: 1 (i.e. when the list is full)
    • Growth Rate: current_size + current_size/2

    Context : JDK 7

    While adding an element into the ArrayList, the following public ensureCapacityInternal calls and the other private method calls happen internally to increase the size. This is what dynamically increase the size of ArrayList. while viewing the code you can understand the logic by naming conventions, because of this reason I am not adding explicit description

    public boolean add(E paramE) {
            ensureCapacityInternal(this.size + 1);
            this.elementData[(this.size++)] = paramE;
            return true;
        }
    
    private void ensureCapacityInternal(int paramInt) {
            if (this.elementData == EMPTY_ELEMENTDATA)
                paramInt = Math.max(10, paramInt);
            ensureExplicitCapacity(paramInt);
        }
    private void ensureExplicitCapacity(int paramInt) {
            this.modCount += 1;
            if (paramInt - this.elementData.length <= 0)
                return;
            grow(paramInt);
        }
    
    private void grow(int paramInt) {
        int i = this.elementData.length;
        int j = i + (i >> 1);
        if (j - paramInt < 0)
            j = paramInt;
        if (j - 2147483639 > 0)
            j = hugeCapacity(paramInt);
        this.elementData = Arrays.copyOf(this.elementData, j);
    }
    
    0 讨论(0)
  • 2020-11-29 18:26

    (oldSize * 3)/2 + 1

    If you are using default constructor then initial size of ArrayList will be 10 else you can pass the initial size of array while creating the object of ArrayList.

    Example: In case default constructor

    List<String> list = new ArrayList<>();
    list.size()
    

    Example: In case parameterized constructor

    List<String> list = new ArrayList<>(5);
    list.size()
    
    0 讨论(0)
  • 2020-11-29 18:27

    From JDK source code, I found below code

    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    
    0 讨论(0)
  • 2020-11-29 18:28

    When we try to add an object to the arraylist,

    Java checks to ensure that there is enough capacity in the existing array to hold the new object. If not, a new array of a greater size is created, the old array is copied to new array using Arrays.copyOf and the new array is assigned to the existing array.

    Look at the code below (taken from Java ArrayList Code at GrepCode.com).

    Check this example

    enter image description here

    Edit:

    public ArrayList() Constructs an empty list with an initial capacity of ten.

    public ArrayList(int initialCapacity) we can specify initial capacity.

    public ArrayList(Collection c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

    Now when we use ArrayList() constructore we get a ArrayList with Size=10 On adding 11th element in the list new Arraylist is created inside ensureCapacity() method.

    Using following formula:

      int newCapacity= (oldCapacity * 3)/2 +1;
    
    0 讨论(0)
  • 2020-11-29 18:28

    In Jdk 1.6: New capacity = (Current Capacity * 3/2) + 1;

    In Jdk 1.7:

    int j = i + (i >> 1); this is same as New capacity = (Current Capacity * 1/2) + Current Capacity;

    ex:size will increase like :10-->15-->22-->33

    0 讨论(0)
提交回复
热议问题