📅  最后修改于: 2023-12-03 15:01:50.561000             🧑  作者: Mango
remove()
method in ArrayList and LinkedList in JavaThe ArrayList
and LinkedList
are two commonly used classes in Java for implementing dynamic arrays and linked lists, respectively. Both classes provide a remove()
method that allows removing elements from the list. In this article, we will explore the remove()
method in both classes and provide some examples to illustrate its usage.
remove()
methodThe ArrayList
class in Java provides the remove()
method that allows removing elements from the list based on index or object value. Here is the syntax of the remove()
method in ArrayList
:
public E remove(int index)
This method takes the index of the element to be removed as a parameter and returns the element that was removed. The size of the ArrayList
is automatically decremented by 1 after removing the element. If the index is out of bounds (i.e., less than 0 or greater than or equal to the size of the list), an IndexOutOfBoundsException
is thrown.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
System.out.println("Before removal: " + fruits);
String removedElement = fruits.remove(1);
System.out.println("Removed element: " + removedElement);
System.out.println("After removal: " + fruits);
}
}
Output:
Before removal: [Apple, Banana, Mango]
Removed element: Banana
After removal: [Apple, Mango]
remove()
methodThe LinkedList
class in Java provides multiple remove()
methods that allow removing elements from the list based on different conditions. Here are the syntaxes of the remove()
methods in LinkedList
:
public E remove(int index)
This method takes the index of the element to be removed as a parameter and returns the element that was removed. The size of the LinkedList
is automatically decremented by 1 after removing the element.
public boolean remove(Object o)
This method takes the object value that needs to be removed as a parameter. It returns true
if the element is successfully removed, otherwise false
.
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> colors = new LinkedList<>();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
System.out.println("Before removal: " + colors);
String removedElement = colors.remove(1);
System.out.println("Removed element by index: " + removedElement);
boolean isRemoved = colors.remove("Red");
System.out.println("Element 'Red' removed: " + isRemoved);
System.out.println("After removal: " + colors);
}
}
Output:
Before removal: [Red, Blue, Green]
Removed element by index: Blue
Element 'Red' removed: true
After removal: [Green]
In this article, we discussed the remove()
method available in ArrayList
and LinkedList
classes in Java. We explored the syntax and usage of the remove()
method with relevant examples. The remove()
method is a useful operation that allows programmers to remove elements from the dynamic arrays (ArrayList
) and linked lists (LinkedList
) in Java.