2

I know ArrayList can contain "any number of items" (as long you have the memory for it). However, I was wondering if the initial size of the list is different for each machine running it. If I have 8GB of RAM vs 4GB RAM does this change the intial size? Thanks!

ArrayList list = new ArrayList<Integer>();
2
  • 3
    By default its of size 10 and keep growing by size of 10 Commented Oct 2, 2014 at 19:15
  • 1
    You could have an 80 GB heap and it would be same. Commented Oct 2, 2014 at 19:20

4 Answers 4

3

The initial size of the ArrayList if the no-arg constructor is used quoting from the javadoc:

Constructs an empty list with an initial capacity of ten.

So it does not depend on anything, it doesn't depend on the RAM size either.

However there are some clever optimization going on in the background. If you check the Oracle implementation of ArrayList, you will see that in this case an initial empty internal array will be used so no array will be allocated until you actually add some elements to the list - in which case an array of size 10 will be created.

Once you attempt to add the 11th element, the internal array will be "resized". The new size is also implementation dependent, Oracle uses a 50% increment in the 1.7.0 version, so adding the 11th element will cause a new array to be allocated with the size of 15.

Going behind the scene

For the curious ones, you can use the following method to query the size of the internal array of the ArrayList (the solution uses reflection):

public static int getCap(ArrayList<?> list) throws Exception {
    Field f = list.getClass().getDeclaredField("elementData");
    f.setAccessible(true);
    Object[] o = (Object[]) f.get(list);
    return o.length;
}

Test results

ArrayList<String> list = new ArrayList<>();
System.out.println(getCap(list));   // Prints 0

list.add("");
System.out.println(getCap(list));   // Prints 10

for (int i = 1; i < 11; i++)
    list.add("");
System.out.println(getCap(list));   // Prints 15

for (int i = 11; i < 16; i++)
    list.add("");
System.out.println(getCap(list));   // Prints 22
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is exactly the answer I was looking for :)
3

The initial capacity is by default 10. You can always use the constructor that accepts a capacity parameter in order to start with a different capacity.

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    this(10);
}

Comments

2

It is by default created with an initial capacity of 10 (regardless of available RAM).

Comments

1

By default ArrayList is of size 10 object and keep growing by size of 10. Eg

ArrayList list = new ArrayList<Integer>();

Here the size of list will be 10. Once you add the 11 object the size will be incremented by 10 and will become 20.
You can change the initial size by invoking the constructor with a size. See the example.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.