See Example :
public class Example {
public static void main(String[] args) {
ArrayList<String> arrlist = getArrayList();
}
public static ArrayList<String> getArrayList(){
ArrayList<String> arrlist = new ArrayList<String>();
//add method for String ArrayList
arrlist.add("Item1");
arrlist.add("Item2");
arrlist.add("Item3");
arrlist.add("Item4");
arrlist.add("Item5");
System.out.println(" Size of ArrayList = "+ arrlist.size());
arrlist.add("Apple");
arrlist.add("Orange");
System.out.println("after add new size of Arraylist = "+ arrlist.size());
//remove specific index
arrlist.remove(5);
//remove specific element
arrlist.remove("Orange");
System.out.println("after remove size of Arraylist "+ arrlist.size());
return arrlist;
}
}
//result
Size of ArrayList = 5
after add new size of Arraylist = 7
after remove size of Arraylist 5
EmoticonEmoticon