java.util.Vector.indexOf() 方法返回向量中指定元素第一次出现的索引,从指定索引向前搜索。如果向量中不存在元素,则返回 -1。
语法
public int indexOf(Object obj, int index)
参数
obj | 指定要在向量中搜索的元素。 |
index | 指定索引到search 搜索自。 |
返回值
返回向量中第一次出现指定元素的索引,向前搜索指定索引,如果向量中不存在元素,则为 -1。
异常
不适用。
示例:
在下面的示例中, java.util.Vector.indexOf() 方法用于查找向量 MyVector 中指定元素第一次出现的索引,从指定索引处向前搜索。
import java.util.*;
public class MyClass {
public static void main(String[] args) {
//创建向量
Vector<Integer> MyVector = new Vector<Integer>();
//填充向量
MyVector.add(10);
MyVector.add(30);
MyVector.add(20);
MyVector.add(50);
MyVector.add(30);
System.out.print("30 appeared first time after index=2 at index=");
System.out.print(MyVector.indexOf(30, 2));
}
}
上述代码的输出将是:
30 appeared first time after index=2 at index=4