Arraylist: How Does the Size Increase

ArrayList: how does the size increase?

A new array is created and the contents of the old one are copied over. That's all you know at the API level. Quoting from the docs (my emphasis):

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

In terms of how it actually happens with a specific implementation of ArrayList (such as Sun's), in their case you can see the gory details in the source. But of course, relying on the details of a specific implementation isn't usually a good idea...

How to increase the size of an arraylist

You have established the initial capacity of the ArrayList, but there are still no items in the list. You can't insert an item before index 1 if it doesn't exist yet.

You must use the one-argument add method to append to the end of the list, or supply 0 as the index in the two-argument add method, so that there is something in the list.

Can the size of an ArrayList be changed after it is initialized?

False. To quote from javadoc, it explains array list to be a

Resizable-array implementation of the List interface.

Further it also explains

Each ArrayList instance has a capacity. The capacity is the size of
the array used to store the elements in the list. It is always at
least as large as the list size. As elements are added to an
ArrayList, its capacity grows automatically.

Also, for an ArrayList, size can not be set while initializing. However the initial capacity can be set. Size is the number of elements in the list.

ArrayList capacity size increasing strange behaviour

ArrayList code is optimized to start the capacity at 10, and grow it by 1.5 times each time when more space is needed.

You can detect precise grows points with a modified version of your program:

public void addElements() throws Exception {
int lastCap = -1;
for (int j = 0; j < 1000000; j++) {
this.normalList.add(j);
int cap = getCapacity(this.normalList);
if (cap != lastCap) {
System.out.println("size:" + normalList.size() + " capacity:" + cap);
lastCap = cap;
}
}
}

int getCapacity(List al) throws Exception {
Field field = ArrayList.class.getDeclaredField("elementData");
field.setAccessible(true);
return ((Object[]) field.get(al)).length;
}

This prints the following numbers:

size:1 capacity:10
size:11 capacity:15
size:16 capacity:22
size:23 capacity:33
size:34 capacity:49
size:50 capacity:73
size:74 capacity:109
... // And so on

The source code responsible for growing the list is in ensureCapacity method, it goes as follows:

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

This is an equivalent of integer multiplication by 1.5.

Can arraylist increase its size?

You can initialize an Array List with initial capacity by :

ArrayList<String> array = new ArrayList<>(1000);

The need to initialize is for performance enhancement. This may help
https://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Initial size for the ArrayList

You're confusing the size of the array list with its capacity:

  • the size is the number of elements in the list;
  • the capacity is how many elements the list can potentially accommodate without reallocating its internal structures.

When you call new ArrayList<Integer>(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty.

One way to add ten elements to the array list is by using a loop:

for (int i = 0; i < 10; i++) {
arr.add(0);
}

Having done this, you can now modify elements at indices 0..9.

Does the capacity of ArrayList decrease when we remove elements?

It doesn't decrease this automatically. From the doc.

    public void trimToSize() 

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.

For loop: how is changing ArrayList size interpreted?

At the beginning of each iteration, the middle expression is evaluated. If it is true, the loop continues. If it is false, the loop breaks. So it will re-evaluate the size every iteration.

To make it loop the number of times of the original size of the list, do something like this:

int size = someArrayList.size();
for (i = 0 ; i < size ; i++) { ... }

Or if you want to get fancy:

for (i = 0, size = someArrayList.size() ; i < size ; i++) { ... }


Related Topics



Leave a reply



Submit