I get exception Exception in thread \"main\" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
for the below code. But couldn\'t understand why.
You can initialize the size (not the capacity) of an ArrayList in this way:
ArrayList list = new ArrayList(Arrays.asList(new T[size]));
in your case:
ArrayList s = new ArrayList(Arrays.asList(new String[10]));
this creates an ArrayList with 10 null elements, so you can add elements in random order within the size (index 0-9).