java.util.Vector.lastIndexOf() 方法

描述

这是 lastIndexOf() 方法的另一个变体。它从指定的索引开始向后搜索指定的对象,并返回一个索引。


声明

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

public int lastIndexOf(Object elem,int index)

参数

  • elem − 这是所需的组件

  • index − 这是开始搜索的索引。


返回值

该方法调用返回指定对象在此向量中的最后一次出现的索引,该索引的位置小于或等于向量中的索引。


异常

IndexOutOfBoundsException − 如果 index 大于或等于此向量的当前大小,则会引发此异常。


示例

下面的例子展示了 java.util.Vector.lastIndexOf() 方法的使用。

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>(6);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      vec.add(2);
      vec.add(1);

      /**let us print the last index of 1
      * starting search from 4th position
      */
      System.out.println("index is: "+vec.lastIndexOf(1,4));
   }
}

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

index is :3