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.
Answer from NPE on Stack OverflowYou'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.
If you want a list with a predefined size you can also use:
List<Integer> arr = Arrays.asList(new Integer[10]);
ArrayList default initialization size
java - How to initialize an ArrayList with a certain size and directly access its elements - Stack Overflow
Why isn't initializing ArrayList size working? - Processing 2.x and 3.x Forum
[Java] How to write .size() method for an ArrayList?
I'm reading that an ArrayList has a default size of 10 once initialized. If that is the case why does the following code throw IndexOutOfBoundsException ?
public class Test {
public static void main(String[] args) {
List list = new ArrayList < > ();
list.add(0,"ONE");
list.add(1, "TWO");
list.add(5, "THREE");
System.out.println(list);
}
}I'm taking the liberty of repeating bcsb1001's solution, which is perhaps obscured by various comments. In order to initialize an array of a fixed size with valid placeholder values:
new ArrayList (Collections.nCopies (n, null));
This idea was the best I could find in various searches, maybe it will work for others too.
If you take a look at Java API documentation, you will see that there are 3 constructors provided for ArrayList:
ArrayList()
ArrayList(Collection c) - Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayList(int initialCapacity) - Constructs an empty list with the specified initial capacity.
You can use second of listed constructors if you need to fill ArrayList's slots in the place of its definition. You need to be aware that there needs to be passed a Collection, so you can do something like that, for example:
ArrayList<Integer> al = new ArrayList<>(Arrays.asList(1,2,3,4,5);
Now size() of al is 5 and it is filled with numbers 1,2,3,4 and 5.
Also, you need to be aware that ArrayList doesn't work like Arrays and it's size is not fixed - it changes due to adding or removing items.