Simple add() method is used for adding an element at the end of the list however there is another variant of add method which is used for adding an element to the specified index.
public void add(int index, Object element)
This method adds the element at the given index.
Example
package beginnersbook.com; import java.util.ArrayList; public class AddMethodExample { public static void main(String[] args) { // ArrayList of String type ArrayList<String> al = new ArrayList<String>(); // simple add() methods for adding elements at the end al.add("Hi"); al.add("hello"); al.add("String"); al.add("Test"); //adding element to the 4th position //4th position = 3 index as index starts with 0 al.add(3,"Howdy"); System.out.println("Elements after adding string Howdy:"+ al); //adding string to 1st position al.add(0, "Bye"); //Print System.out.println("Elements after adding string bye:"+ al); } }
Output:
Elements after adding string Howdy:[Hi, hello, String, Howdy, Test] Elements after adding string bye:[Bye, Hi, hello, String, Howdy, Test]
Reference
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(int, E)
Enamul Haque says
Thank For developing The nice Site.
It is helpful every method description with example.
Thanks Again as a java developer.