remove the given element from an ArrayList

Java Static & Dynamic Data Structures, Declare different type of Arrays, Read Data, Print Arrays, Copy Arrays and Create Different Type of ArrayLists and conduct ArrayList Operations.
Post Reply
manoj
Posts: 87
Joined: Fri Nov 23, 2018 5:29 am

remove the given element from an ArrayList

Post by manoj » Wed Dec 05, 2018 9:48 am

How do you remove the given element from an ArrayList?

swathi
Posts: 134
Joined: Fri Nov 23, 2018 2:52 am

Re: remove the given element from an ArrayList

Post by swathi » Wed Dec 05, 2018 9:49 am

remove(Object obj) method removes the first occurrence of the specified element ‘obj‘. If that element doesn’t exist, ArrayList will be unchanged.

public class MainClass
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();

list.add("AAA");

list.add("BBB");

list.add("ccc");

list.add("DDD");

list.add("e");

System.out.println(list); //Output : [AAA, BBB, ccc, DDD, e]

//Removing an element from position 2

list.remove(2);

System.out.println(list); //Output : [AAA, BBB, DDD, e]

//Removing an element from position 3

list.remove(3);

System.out.println(list); //Output : [AAA, BBB, DDD]
}
}

satyanarayanareddy
Posts: 24
Joined: Thu Jan 03, 2019 3:54 am

Re: remove the given element from an ArrayList

Post by satyanarayanareddy » Mon Jan 21, 2019 3:39 am

There are two way to remove an element from ArrayList.
1. By using remove() methods :
ArrayList provides two overloaded remove() method.
a. remove(int index) : Accept index of object to be removed.
b. remove(Obejct obj) : Accept object to be removed.

Example:
--------
import java.util.List;
import java.util.ArrayList;

public class AAA
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);

// This makes a call to remove(int) and
// removes element 20.
al.remove(1);

// Now element 30 is moved one position back
// So element 30 is removed this time
al.remove(1);

System.out.println("Modified ArrayList : " + al);
}
}

Output: Modified ArrayList : [10, 1, 2]

2. Using Iterator.remove() method :
It is not recommended to use ArrayList.remove() when iterating over elements. This may lead to ConcurrentModificationException (Refer this for a sample program with this exception). When iterating over elements, it is recommended to use Iterator.remove() method .

Example:
--------
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class GFG
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);

// Remove elements smaller than 10 using
// Iterator.remove()
Iterator itr = al.iterator();
while (itr.hasNext())
{
int x = (Integer)itr.next();
if (x < 10)
itr.remove();
}

System.out.println("Modified ArrayList : "
+ al);
}
}

Output: Modified ArrayList : [10, 20, 30]

Post Reply

Return to “Java Arrays and ArrayList”