arraylist how to add at 0 java

Solutions on MaxInterview for arraylist how to add at 0 java by the best coders in the world

showing results for - "arraylist how to add at 0 java"
Stefano
12 Nov 2016
1//The add(int index, E element) method of Java ArrayList class 
2//inserts a specific element in a specific index of ArrayList. 
3//It shifts the element of indicated 
4//index if exist and subsequent elements to the right.
5List<String> colors = new ArrayList<>();
6colors.add("red");      // ["red"]  
7colors.add("blue");     // ["red" , "blue"]  
8colors.add(1, "white"); // ["red" , "white", "blue"]  
9colors.add(0, "black"); // ["black", "red" , "white", "blue"]