Java LinkedList removeFirstOccurrence()方法

Java LinkedList removeFirstOccurrence()方法

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

语法:

LinkedListObject.removeFirstOccurrence(Object element)

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

程序1 :

// 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<String> list = new LinkedList<String>();
 
        // 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);
    }
}

输出

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]

示例2 :

// 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<Integer> list = new LinkedList<Integer>();
 
        // 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]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程