I get an exception Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0for the code below. But I could not understand why.
public class App {
public static void main(String[] args) {
ArrayList<String> s = new ArrayList<>();
s.add(1,"Elephant");
System.out.println(s.size());
}
}
Update
I can make it work, but I'm trying to understand the concepts, so I changed the ad below, but didn't work.
ArrayList<String> s = new ArrayList<>(10)
source
share