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]
}
}

Post Reply

Return to “Java Arrays and ArrayList”