Java 集合 从LinkedList删除特定元素

在上一篇文章中,我们在一篇上分享了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
赞(0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Java 集合