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 :

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class ArrayListRemoveAtIndex {
  4.     public static void main(String[] args) {
  5.     ArrayList<String> list = new ArrayList<String>();
  6.     list.add("Java");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("c");
  10. System.out.println("List before removing elements");
  11. System.out.println(list);
  12. list.remove(2);
  13. System.out.println("List after removing elements");
  14. System.out.println(list);
  15.     }
  16. }
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 :

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class ArrayListRemoveElement {
  4.     public static void main(String[] args) {
  5.     ArrayList<String> list = new ArrayList<String>();
  6.     list.add("java");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("java");
  10. System.out.println("List before removing elements");
  11. System.out.println(list);
  12. list.remove("java");
  13. System.out.println("List after removing elements");
  14. System.out.println(list);
  15.     }
  16. }
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 :

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class ArrayListRemoveAllElements {
  4.     public static void main(String[] args) {
  5.     ArrayList<String> list1 = new ArrayList<String>();
  6.     list1.add("java");
  7. list1.add("c++");
  8. list1.add("python");
  9. list1.add("c");
  10. ArrayList<String> list2 = new ArrayList<String>();
  11. list2.add("c++");
  12. list2.add("python");
  13. System.out.println("List before removing elements");
  14. System.out.println(list1);
  15. list1.removeAll(list2);
  16. System.out.println("List after removing elements");
  17. System.out.println(list1);
  18.     }
  19. }
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

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on