当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。