java.util.Vector.indexOf() 方法
描述
indexOf(Object elem) 方法用于搜索给定参数的第一次出现。使用equals 方法测试是否相等。
声明
以下是 java.util.Vector.indexOf() 方法的声明
public int indexOf(Object elem)
参数
elem − 输入参数是一个对象
返回值
返回值是该向量中参数第一次出现的索引。如果找不到对象,则返回-1。
异常
NA
示例
下面的例子展示了 java.util.Vector.indexOf() 方法的使用。
package com.tutorialspoint;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec with an initial capacity of 4
Vector<Integer> vec = new Vector<Integer>(4);
// use add() method to add elements in the vector
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(3);
// let us get the index of 3
System.out.println("Index of 3 is :"+vec.indexOf(3));
}
}
让我们编译并运行上面的程序,这将产生以下结果. Please note the index of first occurrence of 3 is 1 so 1 is printed instead of 3.
Index of 3 is :1

