Provide List Bandwidth

I looked at how to create my own list and landed on this site http://www.vogella.com/tutorials/JavaDatastructureList/article.html , where they had the method below.

private void ensureCapa() {
    int newSize = elements.length * 2;
    elements = Arrays.copyOf(elements, newSize);
}

I found similar methods in many other sites and realized that it works. But I do not understand why the length is multiplied by 2 (elements.length * 2). Is there a specific reason or does it depend on the type of data?

Thanks in advance.

+4
source share
3 answers

Doubling the ability of a list when it is full is performed for several reasons.

@jheimbouch @user3437460, . . .

. , ( ), , , O(1) ( Amortized Constant Time).

, n. , , - , . , , . , .

, http://anh.cs.luc.edu/363/notes/06A_Amortizing.html

, .

+4

, . , elements.length + 1, , .

, , .

+1

, 2 (elements.length * 2). ?

, 5. , , ( , ).

, 1000 , , , 1000 . , , , .

When you add too much, you waste memory, but if you add too little, you probably need to copy the array too much, which is inefficient because it means that you are more likely to adjust the current array when you add new elements.

0
source

Source: https://habr.com/ru/post/1627453/


All Articles