从链表中删除元素
LinkedList 类的 remove() 方法接受一个元素作为参数,并将其从当前链表中删除。
您可以使用此方法从链表中删除元素。
示例
import java.util.LinkedList;
public class RemovingElements {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add("Mangoes");
linkedList.add("Grapes");
linkedList.add("Bananas");
linkedList.add("Oranges");
linkedList.add("Pineapples");
System.out.println("Contents of the linked list :"+linkedList);
linkedList.remove("Grapes");
System.out.println("Contents of the linked list after removing the specified element :"+linkedList);
}
}
输出
Contents of the linked list :[Mangoes, Grapes, Bananas, Oranges, Pineapples] Contents of the linked list after removing the specified element :[Mangoes, Bananas, Oranges, Pineapples]
广告