📜  Java中的 LinkedList removeFirstOccurrence() 方法

📅  最后修改于: 2022-05-13 01:55:21.214000             🧑  作者: Mango

Java中的 LinkedList removeFirstOccurrence() 方法

Java.util.LinkedList.removeFirstOccurrence() 用于从列表中删除指定元素的第一次出现。如果没有出现指定元素,则列表保持不变。

语法

LinkedListObject.removeFirstOccurrence(Object element)

参数:该方法采用一个要从列表中删除的参数元素。 Object 类型应与列表中的元素相同。
返回值:如果列表包含指定元素并被删除,则该方法返回布尔值 True,否则返回 false。
下面的程序说明了 removeFirstOccurrence() 方法:
程序 1:

java
// Java code to demonstrate removeFirstOccurrence() method
import java.util.LinkedList;
public class GfG {
    // Main method
    public static void main(String[] args)
    {
 
        // Creating a LinkedList object
        LinkedList list = new LinkedList();
 
        // Adding an element at the last
        list.addLast("one");
 
        // Adding an element at the last
        list.addLast("two");
 
        // Adding an element at the last
        list.addLast("three");
 
        // Adding an element at the last
        list.addLast("one");
 
        System.out.print("List before removing the "+
                   "first Occurrence of \"one\" : ");
 
        // Printing the list
        System.out.println(list);
 
        // Removing first occurrence of one.
        boolean returnValue = list.removeFirstOccurrence("one");
 
        // Printing the returned value
        System.out.println("Returned Value : " + returnValue);
 
        System.out.print("List after removing the"+
                            " first Occurrence of \"one\" : ");
 
        // Printing the list
        System.out.println(list);
    }
}


Java
// Java code to demonstrate removeFirstOccurrence method in LinkedList
 
import java.util.LinkedList;
 
public class GfG {
    // Main method
    public static void main(String[] args)
    {
 
        // Creating a LinkedList object
        LinkedList list = new LinkedList();
 
        // Adding an element at the last
        list.addLast(10);
 
        // Adding an element at the last
        list.addLast(20);
 
        // Adding an element at the last
        list.addLast(30);
 
        // Adding an element at the last
        list.addLast(10);
 
        System.out.print("List before removing the"+
                  " first Occurrence of \"10\" : ");
 
        // Printing the list
        System.out.println(list);
 
        // Removing first occurrence of one.
        boolean returnValue = list.removeFirstOccurrence(10);
 
        // Printing the returned value
        System.out.println("Returned Value : " + returnValue);
 
        System.out.print("List after removing the"+
                           " first Occurrence of \"10\" : ");
 
        // Printing the list
        System.out.println(list);
    }
}


输出:
List before removing the first Occurrence of "one" : [one, two, three, one]
Returned Value : true
List after removing the first Occurrence of "one" : [two, three, one]

方案二:

Java

// Java code to demonstrate removeFirstOccurrence method in LinkedList
 
import java.util.LinkedList;
 
public class GfG {
    // Main method
    public static void main(String[] args)
    {
 
        // Creating a LinkedList object
        LinkedList list = new LinkedList();
 
        // Adding an element at the last
        list.addLast(10);
 
        // Adding an element at the last
        list.addLast(20);
 
        // Adding an element at the last
        list.addLast(30);
 
        // Adding an element at the last
        list.addLast(10);
 
        System.out.print("List before removing the"+
                  " first Occurrence of \"10\" : ");
 
        // Printing the list
        System.out.println(list);
 
        // Removing first occurrence of one.
        boolean returnValue = list.removeFirstOccurrence(10);
 
        // Printing the returned value
        System.out.println("Returned Value : " + returnValue);
 
        System.out.print("List after removing the"+
                           " first Occurrence of \"10\" : ");
 
        // Printing the list
        System.out.println(list);
    }
}
输出:
List before removing the first Occurrence of "10" : [10, 20, 30, 10]
Returned Value : true
List after removing the first Occurrence of "10" : [20, 30, 10]