Remove ArrayList elements
We can use remove() method of ArrayList to delete/remove elements from ArrayList
There are 4 overloaded “remove” methods defined in ArrayList
1) Object remove(int index);
2) boolean remove(Object o);
3) boolean removeAll(Collection> c);
4) protected void removeRange(int fromIndex, int toIndex);
1) Object remove(int index)
Removes the element at the specified position in the list.
Example :
- import java.util.ArrayList;
- import java.util.List;
- public class ArrayListRemoveAtIndex {
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("Java");
- list.add("c++");
- list.add("python");
- list.add("c");
- System.out.println("List before removing elements");
- System.out.println(list);
- list.remove(2);
- System.out.println("List after removing elements");
- System.out.println(list);
- }
- }
import java.util.ArrayList; import java.util.List; public class ArrayListRemoveAtIndex { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Java"); list.add("c++"); list.add("python"); list.add("c"); System.out.println("List before removing elements"); System.out.println(list); list.remove(2); System.out.println("List after removing elements"); System.out.println(list); } }
Note :
We can see that element at index 2 in the list is removed
2) boolean remove(Object o)
Removes the first occurrence of the specified element from the list, if it is present.
Example :
- import java.util.ArrayList;
- import java.util.List;
- public class ArrayListRemoveElement {
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("java");
- list.add("c++");
- list.add("python");
- list.add("java");
- System.out.println("List before removing elements");
- System.out.println(list);
- list.remove("java");
- System.out.println("List after removing elements");
- System.out.println(list);
- }
- }
import java.util.ArrayList; import java.util.List; public class ArrayListRemoveElement { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("java"); list.add("c++"); list.add("python"); list.add("java"); System.out.println("List before removing elements"); System.out.println(list); list.remove("java"); System.out.println("List after removing elements"); System.out.println(list); } }
Note :
We can see that , first occurance of element “java” is removed from the list
3) boolean removeAll(Collection> c)
Removes from the list all of its elements that are contained in the specified collection.
Example :
- import java.util.ArrayList;
- import java.util.List;
- public class ArrayListRemoveAllElements {
- public static void main(String[] args) {
- ArrayList<String> list1 = new ArrayList<String>();
- list1.add("java");
- list1.add("c++");
- list1.add("python");
- list1.add("c");
- ArrayList<String> list2 = new ArrayList<String>();
- list2.add("c++");
- list2.add("python");
- System.out.println("List before removing elements");
- System.out.println(list1);
- list1.removeAll(list2);
- System.out.println("List after removing elements");
- System.out.println(list1);
- }
- }
import java.util.ArrayList; import java.util.List; public class ArrayListRemoveAllElements { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<String>(); list1.add("java"); list1.add("c++"); list1.add("python"); list1.add("c"); ArrayList<String> list2 = new ArrayList<String>(); list2.add("c++"); list2.add("python"); System.out.println("List before removing elements"); System.out.println(list1); list1.removeAll(list2); System.out.println("List after removing elements"); System.out.println(list1); } }
Note :
We can see that “removeAll()” method has removed all of its elements “c++” and “python” from the main list
4) protected void removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
Note :
removeRange(int, int) method is protected We can only invoke it from a subclass of ArrayList or from a class within the same package as ArrayList