當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LinkedList removeFirstOccurrence()用法及代碼示例


java.util.LinkedList.removeFirstOccurrence()用於從列表中刪除第一次出現的指定元素。如果沒有出現指定的元素,則列表保持不變。

用法

LinkedListObject.removeFirstOccurrence(Object element)

參數:該方法采用一個參數element ,該參數element 將從列表中刪除。對象類型應與列表中的元素相同。


返回值:如果列表包含指定的元素並被刪除,則該方法返回boolean 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 objec 
        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 objec 
        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]


相關用法


注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 LinkedList removeFirstOccurrence() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。