See Example:
public void add(int index, Object element);
import java.util.ArrayList;
public class Dmo1 {
public static void main(String[] args) {
ArrayList<String> arrlist = getArrayList();
}
public static ArrayList<String> getArrayList(){
ArrayList<String> al= new ArrayList<String>();
//add method for String ArrayList
al.add("Item3");
al.add("Item2");
al.add("Item1");
al.add("Item5");
al.add("Item6");
System.out.println("Elements of ArrayList of String Type: "+al);
//add element to specific an ArrayList
al.add(3,"Computer");
System.out.println("Add Elements 'Computer' of ArrayList of String Type : "+al);
return al;
}
}
result :
Elements of ArrayList of String Type: [Item3, Item2, Item1, Item5, Item6]
Add Elements 'Computer' of ArrayList of String Type : [Item3, Item2, Item1, Computer, Item5, Item6]
EmoticonEmoticon