目录

int indexOf(Object o, int index)

描述 (Description)

这是之前的indexOf()方法的另一种变体。唯一的区别是搜索给定参数的第一个出现在starts at the index position mentioned as the second parameter

声明 (Declaration)

以下是java.util.Vector.indexOf()方法的声明

public int indexOf(Object elem,int index)

参数 (Parameters)

  • elem - 输入参数是一个对象

  • index - 这是开始搜索的非负索引

返回值 (Return Value)

方法调用返回此向量中第一次出现的对象参数的索引,位于索引处或稍后的向量中。

异常 (Exception)

IndexOutOfBoundsException - 如果索引为负,则抛出此异常

例子 (Example)

以下示例显示了java.util.Vector.indexOf()方法的用法。

package com.iowiki;
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);
      vec.add(2);
      vec.add(3);
      /** let us get the index of 3
      * starting search from 2nd index
      */
      System.out.println("Index of 3 :"+vec.indexOf(3,2));
   }    
}

让我们编译并运行上面的程序,这将产生以下结果。

Index of 3 :3
↑回到顶部↑
WIKI教程 @2018