ArrayList capacity increment equation

前端 未结 4 820
一整个雨季
一整个雨季 2021-01-19 14:21

In the JDK 1.7 into the ArrayList.java the method ensureCapacity increments the array capacity using the following expression: int newCapacity = oldCapaci

4条回答
  •  北海茫月
    2021-01-19 14:50

    You're understanding is correct, newCapacity is 50% more than oldCapacity

    In Java 6 newCapacity is calculated as

    int newCapacity = (oldCapacity * 3)/2 + 1;
    

    This is the beauty of an open source language such as Java, you can see the implementation - if it doesn't fit your requirements, you can implement your own.

提交回复
热议问题