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 - Initialization of an ArrayList in one line - Stack Overflow
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.
It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
Or if you have only one element:
List<String> places2 = Collections.singletonList("Buenos Aires");
This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).
To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:
ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
And import the correct package:
import java.util.Arrays;
Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
The catch is that there is quite a bit of typing required to refer to that list instance.
There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):
ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};
However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.
What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):
List<String> list = ["A", "B", "C"];
Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.