lastIndexOf(對象元素) 方法的LinkedList 類存在於裏麵java.util包用於檢查並查找列表中特定元素的出現。如果該元素存在於列表中,則 lastIndexOf() 方法返回該元素最後一次出現的索引,否則返回 -1。此方法用於查找 LinkedList 中特定元素的最後一次出現。
用法:
LinkedList.lastIndexOf(Object element)
參數:參數元素是 LinkedList 類型。
It refers to the element whose last occurrence is required to be checked.
返回值:該元素在列表中最後一次出現的位置,否則如果該元素不存在於列表中,則該方法返回 -1。返回值是整型。
例子:
Java
// Java Program to Illustrate lastIndexOf() Method
// of LinkedList class
// Importing required classes
import java.io.*;
import java.util.LinkedList;
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty LinkedList of string type
LinkedList<String> list = new LinkedList<String>();
// Adding elements in the list
// using add() method
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Displaying the current elements inside LinkedList
System.out.println("LinkedList:" + list);
// The last position of an element is returned
// using lastIndexOf() method and
// displaying on the console
System.out.println(
"Last occurrence of Geeks is at index: "
+ list.lastIndexOf("Geeks"));
System.out.println(
"Last occurrence of 10 is at index: "
+ list.lastIndexOf("10"));
}
}
輸出:
LinkedList:[Geeks, for, Geeks, 10, 20] Last occurrence of Geeks is at index: 2 Last occurrence of 10 is at index: 3
相關用法
- Java LinkedList lastIndexOf()用法及代碼示例
- Java LinkedList listIterator()用法及代碼示例
- Java LinkedList add()用法及代碼示例
- Java LinkedList addAll()用法及代碼示例
- Java LinkedList addFirst()用法及代碼示例
- Java LinkedList addLast()用法及代碼示例
- Java LinkedList clear()用法及代碼示例
- Java LinkedList clone()用法及代碼示例
- Java LinkedList contains()用法及代碼示例
- Java LinkedList descendingIterator()用法及代碼示例
- Java LinkedList element()用法及代碼示例
- Java LinkedList get()用法及代碼示例
- Java LinkedList getFirst()用法及代碼示例
- Java LinkedList getLast()用法及代碼示例
- Java LinkedList indexOf()用法及代碼示例
- Java LinkedList pop()用法及代碼示例
- Java LinkedList push()用法及代碼示例
- Java LinkedList remove()用法及代碼示例
- Java LinkedList removeFirst()用法及代碼示例
- Java LinkedList removeFirstOccurrence()用法及代碼示例
- Java LinkedList removeLast()用法及代碼示例
- Java LinkedList removeLastOccurrence()用法及代碼示例
- Java LinkedList set()用法及代碼示例
- Java LinkedList size()用法及代碼示例
- Java LinkedList spliterator()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 LinkedList lastIndexOf() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。