當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Vector.indexOf()用法及代碼示例


描述

這是先前 indexOf() 方法的另一個變體。唯一的區別是搜索給定參數的第一次出現starts at the index position mentioned as the second parameter

聲明

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

public int indexOf(Object elem,int index)

參數

  • elem− 輸入參數是一個對象

  • index- 這是開始搜索的非負索引

返回值

方法調用返回此向量中對象參數在位置 index 或向量中稍後出現的第一次出現的索引。

異常

IndexOutOfBoundsException- 如果索引為負,則拋出此異常

示例

下麵的例子展示了 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);
      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

相關用法


注:本文由純淨天空篩選整理自 java.util.Vector.indexOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。