public boolean remove(Object o):从该列表中删除指定元素的第一个匹配项(如果存在)。如果此列表不包含该元素,则不会更改。如果此列表包含指定的元素,则返回true(或等效地,如果此列表因调用而更改)。
import java.util.LinkedList;
public class RemoveExample {
public static void main(String[] args) {
// Create a LinkedList
LinkedList<String> linkedlist = new LinkedList<String>();
// Add elements to LinkedList
linkedlist.add("Item1");
linkedlist.add("Item2");
linkedlist.add("Item3");
linkedlist.add("Item4");
linkedlist.add("Item5");
// Displaying Elements before remove
System.out.println("Before Remove:");
for(String str: linkedlist){
System.out.println(str);
}
// Removing "Item4" from the list
linkedlist.remove("Item4");
// LinkedList elements after remove
System.out.println("\nAfter Remove:");
for(String str2: linkedlist){
System.out.println(str2);
}
}
}
输出:
Before Remove:
Item1
Item2
Item3
Item4
Item5
After Remove:
Item1
Item2
Item3
Item5