In the previous article we have discussed about Java ArrayList sort() Method with Example
In this article we are going to see the use ArrayList remove() method along with suitable examples by using Java programming language.
Java ArrayList remove() Method with Example
remove( ):
This java.util.ArrayList.remove()
method is used to remove any single element from the arraylist.
It returns true if that specific element is present and successfully removed.
Let’s see remove() method with suitable examples.
Method-1: Java ArrayList remove(Object o) Method
It removes the specified element from the arraylist.
Syntax:
arrayListName.remove(Object o)
Where,
arrayListName
refers to the name of your ArrayList.Object o
refers to the specific element which is to be removed.
Approach:
- Create a new ArrayList of type String.
- Add String elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Then remove the specific elements from the arrayList using
remove(Object o)
method. - Print the new ArrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<String> arr = new ArrayList<String>(); // Adding some elements to the ArrayList arr.add("Hello"); arr.add("this"); arr.add("is"); arr.add("an"); arr.add("example of remove() method"); // Prints the ArrayList elements System.out.println("The elements of ArrayList are: "+arr); // removing the specific element from the ArrayList arr.remove("is"); arr.remove("example of remove() method"); // Prints the new ArrayList elements System.out.println("The new elements of ArrayList are: "+arr); } }
Output: The elements of ArrayList are: [Hello, this, is, an, example of remove() method] The new elements of ArrayList are: [Hello, this, an]
Method-2: Java ArrayList remove(int index) Method
It removes the specified element from the given index present in the arraylist.
Syntax:
arrayListName.remove(int index)
Where,
arrayListName
refers to the name of your ArrayList.int index
refers to the specific index of the element which is to be removed.
Approach:
- Create a new ArrayList of type String.
- Add Integer elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Then remove the specific index elements from the arrayList using
remove()
method. - Print the new ArrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<Integer> arr = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr.add(2); arr.add(4); arr.add(6); arr.add(8); arr.add(10); // Prints the ArrayList elements System.out.println("The elements of ArrayList are: "+arr); // removing the specific index element from the ArrayList arr.remove(2); // Prints the new ArrayList elements System.out.println("The new elements of ArrayList are: "+arr); } }
Output: The elements of ArrayList are: [2, 4, 6, 8, 10] The new elements of ArrayList are: [2, 4, 8, 10]
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs: