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]
相關用法
- Java ArrayDeque removeLastOccurrence()用法及代碼示例
- Java ConcurrentLinkedDeque removeLastOccurrence()用法及代碼示例
- Java LinkedBlockingDeque removeLastOccurrence()用法及代碼示例
- Java BlockingDeque removeLastOccurrence()用法及代碼示例
- Java LinkedList get()用法及代碼示例
- Java LinkedList contains()用法及代碼示例
- Java LinkedList pop()用法及代碼示例
- Java LinkedList add()用法及代碼示例
- Java LinkedList set()用法及代碼示例
- Java LinkedList toArray()用法及代碼示例
- Java LinkedList getFirst()用法及代碼示例
- Java LinkedList getLast()用法及代碼示例
- Java LinkedList size()用法及代碼示例
- Java LinkedList clone()用法及代碼示例
- Java LinkedList indexOf()用法及代碼示例
注:本文由純淨天空篩選整理自psil123大神的英文原創作品 LinkedList removeLastOccurrence() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。