in the last tutorial we learnt how to remove element from specific index, and now we will learn how to remove specific element from Arraylist .
And to remove specific element from Arraylist , we used method remove(Object element) specific element from Arraylist.
public boolean remove(Object element)
See Exmple:
import java.util.ArrayList;
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("ArrayList Items ");
for(String arr : arrlist){
System.out.println(arr);
}
//remove(item3) ,specific element of Arraylist
arrlist.remove("Item3");
System.out.println("ArrayList after remove Item ");
for(String arr : arrlist){
System.out.println(arr);
}
return arrlist;
}
}
//result:
ArrayList Items
Item1
Item2
Item3
Item4
Item5
ArrayList after remove Item
Item1
Item2
Item4
Item5
we saw element "Item3" removed from Arraylist by specific element of Arraylist
Subscribe to:
Post Comments (Atom)
EmoticonEmoticon