Example:
import java.util.*; public class ArrayListExample { public static void main(String args[]) { ArrayList<String> obj = new ArrayList<String>(); /*This is how elements should be added to the array list*/ obj.add("Nabin"); obj.add("Rabin"); obj.add("Sabin"); obj.add("Rashmi"); obj.add("Kripa"); /* Displaying array list elements */ System.out.println("Currently the array list has following elements:"+obj); /*Add element at the given index*/ obj.add(0, "abc"); obj.add(1, "xyz"); /*Remove elements from array list like this*/ obj.remove("Rashmi"); obj.remove("Kripa"); System.out.println("Current array list is:"+obj); /*Remove element from the given index*/ obj.remove(1); System.out.println("Current array list is:"+obj); } }
Output:
Currently the array list has following elements:[Nabin, Rabin, Sabin, Rashmi, Kripa] Current array list is:[abc, xyz, Nabin, Rabin, Sabin] Current array list is:[abc, Nabin, Rabin, Sabin]
No comments:
Post a Comment