Home > Tutorial > Java ArrayList Remove Element Example

Java ArrayList Remove Element Example

This tutorial will provide sample code on how to remove one or more elements from a Java ArrayList object. There are three main methods included in the Java ArrayList Class for removing elements the list. Each of these three methods is explained as well as sample code provided.

Method Arguments Return Description
remove() int Element Will remove an element at the specified position in this list.
remove() Object boolean Will remove the first occurrence of the specified element from this list.
removeAll() Collection boolean Will remove from this list all of its elements that are contained in the specified collection.


Java ArrayList remove(int) Example

The first example will use the ArrayList.remove() method with an int as the parameter. In the sample code we remove the first element of the ArrayList by setting the parameter to 0.

//ArrayList remove(int) example
String elementRemoved = jcdArrayList.remove(0);
System.out.println("\nElement Removed: "+elementRemoved);
Element Removed: The


Java ArrayList remove(Object) Example

The second example will use the ArrayList.remove() method with an Object as the parameter. In the sample code we remove the element of the ArrayList by setting the Object to a String with a value of “The”.

String elementToRemove = "The";
		
if(jcdArrayListExample2.remove(elementToRemove))
{
	System.out.println("Removed Element: "+elementToRemove);
}
else
{
        System.out.println("Couldn't Find Element: "+elementToRemove);
}
Removed Element: The


Java ArrayList remove(Collection) Example

The third example will use the ArrayList.remove() method with a Collection as the parameter. In the sample code we remove all element from ArrayList jcdArrayListExample3 that are included in the List jcdListTmp.

//Make a copy of the original ArrayList
ArrayList<String> jcdArrayListExample3 = new ArrayList<String>(jcdArrayList);
		
//Create a new ArrayList and add two elements
List<String> jcdListTmp = new ArrayList<String>() {{ add("The");  add("Com"); }};
		
//Remove all elements in jcdListTmp from cdArrayListExample3
jcdArrayListExample3.removeAll(jcdListTmp);


Java ArrayList Remove Element Sample Code

package com.jcd.java.tutorials;

import java.util.ArrayList;
import java.util.List;

public class JavaArrayListRemove 
{

	public static void main(String[] args) 
	{
		//Define an ArrayList of Strings
		ArrayList<String> jcdArrayList = new ArrayList<String>();
		
		//Fill the ArrayList with elements
		jcdArrayList.add("The");
		jcdArrayList.add("Java");
		jcdArrayList.add("Code");
		jcdArrayList.add("Depot");
		jcdArrayList.add("Tutorials");
		jcdArrayList.add("Examples");
		jcdArrayList.add("Com");
		
		//Enhance loop through ArrayList
		for(String str : jcdArrayList) 
		{
			System.out.println(str);
		}
		System.out.println("");
		
		//////////ArrayList remove(int) example ///////////////////
		
		//Make a copy of the original ArrayList
		ArrayList<String> jcdArrayListExample1 = new ArrayList<String>(jcdArrayList);
		
		String elementRemoved = jcdArrayListExample1.remove(0);
		System.out.println("Element Removed: "+elementRemoved);
		
		//Enhance loop through ArrayList
		for(String str : jcdArrayListExample1) 
		{
			System.out.println(str);
		}
		System.out.println("");
		
		
		//////////ArrayList remove(Object) example ///////////////////
		
		//Make a copy of the original ArrayList
		ArrayList<String> jcdArrayListExample2 = new ArrayList<String>(jcdArrayList);
		String elementToRemove = "The";
		
		if(jcdArrayListExample2.remove(elementToRemove))
		{
			System.out.println("Removed Element: "+elementToRemove);
		}
		else
		{
			System.out.println("Couldn't Find Element: "+elementToRemove);
		}
		
		
		//Enhance loop through ArrayList
		for(String str : jcdArrayListExample2) 
		{
			System.out.println(str);
		}
		System.out.println("");
		
		
		//////////ArrayList remove(Collection) example ///////////////////
		
		//Make a copy of the original ArrayList
		ArrayList<String> jcdArrayListExample3 = new ArrayList<String>(jcdArrayList);
		
		//Create a new ArrayList and add two elements
		List<String> jcdListTmp = new ArrayList<String>() {{ add("The");  add("Com"); }};
		
		//Remove all elements in jcdListTmp from cdArrayListExample3
		jcdArrayListExample3.removeAll(jcdListTmp);
		
		//Enhance loop through ArrayList
		for(String str : jcdArrayListExample3) 
		{
			System.out.println(str);
		}
		System.out.println("");
		
	}

}
The
Java
Code
Depot
Tutorials
Examples
Com

Element Removed: The
Java
Code
Depot
Tutorials
Examples
Com

Removed Element: The
Java
Code
Depot
Tutorials
Examples
Com

Java
Code
Depot
Tutorials
Examples



Java References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot