I get exception Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 for the below code. But couldn't understand why.
public class App {
    public static void main(String[] args) {
        ArrayList<String> s = new ArrayList<>();
        //Set index deliberately as 1 (not zero)
        s.add(1,"Elephant");
        System.out.println(s.size());                
    }
}
Update
I can make it work, but I am trying to understand the concepts, so I changed declaration to below but didnt work either.
ArrayList<String> s = new ArrayList<>(10)
 
     
     
     
     
     
     
     
     
     
     
    