Initial Size for the Arraylist

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.

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.

initializing size of ArrayLIst

That is because ArrayLists work differently than standard arrays.

When you pass in 10, you're telling it to allocate space for 10 elements, but since you haven't added anything to the ArrayList yet, size() will return 0.

Try adding an element and then printing the size - that should help you understand how it works. When in doubt, check the docs.

ArrayList initial size doesn't work

ArrayList is backed by, as its name indicates, an array. Arrays are fixed-size; when the ArrayList gets near the array's capacity, it resizes it - it creates a new array, copies everything over, and ditches the old array.

When you initialize an ArrayList with a "size", that's actually its initial default capacity. It does not initialize anything into the array. The ArrayList is still empty after you create it; you still need to actually add values to the ArrayList.

Default size of ArrayList

An ArrayList has an internal array to store the list elements.

There is a difference between the two constructor calls in Java 7 and 8:

If you do new ArrayList<>(0) the ArrayList creates a new Object array of size 0.

If you do new ArrayList<>() the ArrayList uses a static empty Object array of size 0 and switches to an own private array once you add items to the list.

EDIT:

The Javadoc of the default ArrayList constructor seems to contradict this.

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
super();
this.elementData = EMPTY_DEFAULTCAPACITY_EMPTY_ELEMENTDATA; // = static, empty
}

But it does not create an element array of length 10 immediately, but instead when you add elements or ensure the capacity:

public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
// any size if not default element table
? 0
// larger than default for default empty table. It's already
// supposed to be at default size.
: DEFAULT_CAPACITY; // = 10

if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}

What's meant by parameter (int initial capacity) in an arraylist

It's the initial capacity, i.e. the number of items that ArrayList will allocate to begin with as the internal storage of items.

ArrayList can contain "any number of items" (as long you have the memory for it) and when doing large initial insertions you can tell ArrayList to allocate a larger storage to begin with as to not waste CPU cycles when it tries to allocate more space for the next item.

Example:

ArrayList list = new ArrayList<Integer>(2);
list.add(1); // size() == 1
list.add(2); // size() == 2, list is "filled"
list.add(3); // size() == 3, list is expanded to make room for the third element

Is there a way of setting the size of an arraylist after declaring it?

You can use ensureCapacity(int)

ArrayList<Integer> al = new ArrayList<>();

al.ensureCapacity(1000);

It is important to note that array lists WILL dynamically resize themselves though.

So I would like to know if there's a way to initialize an arraylist to for example 1000 after it's been declared in a separate statement.

You could always do this, too:

ArrayList<Integer> al;

al = new ArrayList<Integer>(1000);

This is more akin to the regular array initialisation.

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...



Related Topics



Leave a reply



Submit