Java教程

Java Vector indexOf()

indexOf() Java Vector类方法用于获取指定元素首次出现的索引在向量中。 Java indexOf()方法有两种不同类型,可以根据其参数加以区分。这些是:
Java Vector indexOf(Object o)方法 Java Vector indexOf(Object o,int index)方法

indexOf(Object o)方法:

此方法用于获取向量中指定元素首次出现的索引。如果找不到该元素,则返回-1。

indexOf(Object o,int index> c)方法:

此方法用于获取向量中指定元素首次出现的索引。它开始从指定的索引开始向前搜索元素。如果找不到该元素,则返回-1。

语法

以下是 indexOf()方法的声明:
public int indexOf(Object obj)
public int indexOf(Object obj, int index)

参数

参数 说明 必需/可选
index 这是一个索引,从此处开始向前搜索元素。 必需
obj 它是要搜索的元素。 必需

返回

indexOf()方法返回此向量中第一次出现的指定元素的索引,或者返回-1如果向量不包含元素。

异常

IndexOutOfBoundsException -如果数组的索引为该方法,则抛出异常超出范围,即(index <0 || index> size())。

兼容版本

Java 1.2及更高版本
示例1
import java.util.*;
public class VectorIndexOfExample1 {  
    public static void main(String arg[]) {   
        //Create an empty Vector      
        Vector < Integer > in = new Vector < > ();
        //Add elements in the vector
        in.add(100);
        in.add(200);
        in.add(300);
        in.add(400);
            //Obtain an index of first occurrence of the specified element
        System.out.println("Index of element is: " +in.indexOf(300));
          }            
}
输出:
Index of element is: 2

示例2

import java.util.*;
public class VectorIndexOfExample2 {  
    public static void main(String arg[]) {   
        //Create a first empty vector   
          Vector<String> vec = new Vector<>(4); 
          //Add elements in the first vector          
          vec.add("Java");
          vec.add("JavaScript");
          vec.add("Android");
          vec.add("Python");
            //Obtain an index of first occurrence of the specified element
        System.out.println("Index of element is: " +vec.indexOf("C"));
        System.out.println("The element is not found.");
          }            
}
输出:
Index of element is: -1
The element is not found.

示例3

import java.util.*;
public class VectorIndexOfExample3 {  
    public static void main(String arg[]) {   
        //Create a first empty vector   
          Vector<String> vec = new Vector<>(4); 
          //Add elements in the first vector          
          vec.add("Facebook");
          vec.add("Whatsapp");
          vec.add("Twitter");
          vec.add("Instagram");
          vec.add("Skype");
            //this would start searching of element from index -2
        System.out.println("Index of element is found at: " +vec.indexOf("Skype", -2));        
    }            
}
输出:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
    at java.base/java.util.Vector.indexOf(Vector.java:430)
    at myPackage.VectorIndexOfExample3.main(VectorIndexOfExample3.java:14)

示例4

import java.util.*;
public class VectorIndexOfExample4 {  
    public static void main(String arg[]) {   
        //Create an empty Vector      
        Vector < Integer > in = new Vector < > ();
        //Add elements in the vector
        in.add(101);
        in.add(201);
        in.add(301);
        in.add(401);
        in.add(501);
            //this would start searching of element from index 2
          System.out.println("Index of element is found at: " +in.indexOf(401, 2));        
    }            
}
输出:
Index of element is found at: 3

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4