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


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


java.util.concurrent.LinkedList.removeLastOccurrence()方法是Java中的內置方法,它接受參數並刪除該元素在列表中的最後出現。因此,如果該元素不存在於列表中,則它保持不變。

用法:

public boolean removeLastOccurrence(Object o)

參數:該函數接受一個對象elem作為參數,它表示要從列表中刪除最後一個外觀的對象。


返回值:如果elem存在於列表中,則該函數返回true,否則返回false。

以下示例程序旨在說明removeLastOccurrence()方法的使用:

// Java program to demonstrate removeLastOccurrence() 
// method of LinkedList 
  
import java.util.*; 
  
class LinkedListDemo { 
    public static void main(String[] args) 
    { 
        LinkedList<String> list 
            = new LinkedList<String>(); 
  
        list.add("GFG"); 
        list.add("Geeks"); 
        list.add("Gfg"); 
        list.add("gfg"); 
        list.add("Geeks"); 
  
        // Displaying the existing LinkedList 
        System.out.println("LinkedList: "
                           + list); 
  
        // Remove last occurrence of  element 
        list.removeLastOccurrence("Geeks"); 
  
        System.out.println("Removed last occurrence"
                           + " of 'Geeks' from the list"); 
  
        // Displaying the modified LinkedList 
        System.out.println("LinkedList: "
                           + list); 
    } 
}
輸出:
LinkedList: [GFG, Geeks, Gfg, gfg, Geeks]
Removed last occurrence of 'Geeks' from the list
LinkedList: [GFG, Geeks, Gfg, gfg]

示例2:

// Java program to demonstrate removeLastOccurrence() 
// method of LinkedList 
  
import java.util.*; 
  
class LinkedListDemo { 
    public static void main(String[] args) 
    { 
        LinkedList<Integer> 
            list = new LinkedList<Integer>(); 
  
        list.add(12); 
        list.add(280); 
        list.add(12); 
        list.add(1050); 
        list.add(12); 
  
        // Displaying the existing LinkedList 
        System.out.println("LinkedList: "
                           + list); 
  
        // Remove last occurrence of  element 
        list.removeLastOccurrence(12); 
  
        System.out.println("Removed last occurrence"
                           + " of '12' from the list"); 
  
        // Displaying the modified LinkedList 
        System.out.println("LinkedList: "
                           + list); 
    } 
}
輸出:
LinkedList: [12, 280, 12, 1050, 12]
Removed last occurrence of '12' from the list
LinkedList: [12, 280, 12, 1050]


相關用法


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