Java.util.LinkedList.removeLast()方法用於從LinkedList中刪除最後一個元素。刪除元素後,此方法還返回元素。
用法:
LinkedList.removeLast()
參數:此函數不帶任何參數。
返回值:該方法返回列表末尾的最後一個元素或元素。
以下示例程序旨在說明Java.util.LinkedList.removeLast()方法:
// Java code to illustrate removeLast()
import java.io.*;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
// Use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Displaying the list
System.out.println("LinkedList:" + list);
// Remove the tail using removeLast()
System.out.println("The last element is removed: " + list.removeLast());
// Displaying the final list
System.out.println("Final LinkedList:" + list);
// Remove the tail using removeLast()
System.out.println("The last element is removed: " + list.removeLast());
// Displaying the final list
System.out.println("Final LinkedList:" + list);
}
}
輸出:
LinkedList:[Geeks, for, Geeks, 10, 20] The last element is removed: 20 Final LinkedList:[Geeks, for, Geeks, 10] The last element is removed: 10 Final LinkedList:[Geeks, for, Geeks]
相關用法
- Java ConcurrentLinkedDeque removeLast()用法及代碼示例
- Java LinkedBlockingDeque removeLast()用法及代碼示例
- Java ArrayDeque removeLast()用法及代碼示例
- Java LinkedList get()用法及代碼示例
- Java LinkedList pop()用法及代碼示例
- Java LinkedList add()用法及代碼示例
- Java LinkedList set()用法及代碼示例
- Java LinkedList contains()用法及代碼示例
- Java LinkedList removeFirst()用法及代碼示例
- Java LinkedList removeFirstOccurrence()用法及代碼示例
- Java LinkedList push()用法及代碼示例
- Java LinkedList removeLastOccurrence()用法及代碼示例
- Java LinkedList toArray()用法及代碼示例
- Java LinkedList getFirst()用法及代碼示例
- Java LinkedList listIterator()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 LinkedList removeLast() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。