Java.util.Vector.lastIndexOf(Object element)方法用於檢查和查找向量中特定元素的出現。如果向量中存在元素,則lastIndexOf()方法返回該元素最後一次出現的索引,否則返回-1。此方法用於查找向量中特定元素的最後一次出現。
用法:
Vector.lastIndexOf(Object element)
參數:參數element 的類型為Vector。它指的是需要檢查其最後一次出現的元素。
返回值:該方法返回向量中元素最後一次出現的位置。如果Vector中不存在該元素,則該方法返回-1。返回值是整數類型。
以下示例程序旨在說明Java.util.Vector.lastIndexOf()方法:
示例1:
// Java code to illustrate lastIndexOf()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> vec_tor = new Vector<String>();
// Use add() method to add elements in the Vector
vec_tor.add("Geeks");
vec_tor.add("for");
vec_tor.add("Geeks");
vec_tor.add("10");
vec_tor.add("20");
// Displaying the Vector
System.out.println("Vector: " + vec_tor);
// The last position of an element is returned
System.out.println("Last occurrence of Geeks is at index: "
+ vec_tor.lastIndexOf("Geeks"));
System.out.println("Last occurrence of 10 is at index: "
+ vec_tor.lastIndexOf("10"));
}
}
輸出:
Vector: [Geeks, for, Geeks, 10, 20] Last occurrence of Geeks is at index: 2 Last occurrence of 10 is at index: 3
示例2:
// Java code to illustrate lastIndexOf()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<Integer> vec_tor = new Vector<Integer>();
// Use add() method to add elements in the Vector
vec_tor.add(10);
vec_tor.add(22);
vec_tor.add(3);
vec_tor.add(10);
vec_tor.add(20);
// Displaying the Vector
System.out.println("Vector: " + vec_tor);
// The last position of an element is returned
System.out.println("Last occurrence of 10 is at index: "
+ vec_tor.lastIndexOf(10));
System.out.println("Last occurrence of 20 is at index: "
+ vec_tor.lastIndexOf(20));
}
}
輸出:
Vector: [10, 22, 3, 10, 20] Last occurrence of 10 is at index: 3 Last occurrence of 20 is at index: 4
相關用法
- Java LinkedList lastIndexOf()用法及代碼示例
- Java CopyOnWriteArrayList lastIndexOf()用法及代碼示例
- Java Stack lastIndexOf()用法及代碼示例
- Java AbstractSequentialList lastIndexOf()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuilder lastIndexOf()用法及代碼示例
- Java List lastIndexOf()用法及代碼示例
- Java AbstractList lastIndexOf()用法及代碼示例
- Java Stack lastIndexOf(Object, int)用法及代碼示例
- Java Java.lang.String.lastIndexOf()用法及代碼示例
- Java Guava Doubles.lastIndexOf()用法及代碼示例
- Java Guava Floats.lastIndexOf()用法及代碼示例
- Java Guava Shorts.lastIndexOf()用法及代碼示例
- Java Guava Longs.lastIndexOf()用法及代碼示例
- Java Guava Bytes.lastIndexOf()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 Vector lastIndexOf() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。