Java中的向量indexOf()方法
Java.util.vector.indexOf(Object element)方法用于检查和查找向量中特定元素的出现。如果元素存在,则返回该元素第一次出现的索引,否则如果向量不包含该元素,则返回 -1。
句法:
Vector.indexOf(Object element)
参数: Vector 类型的元素,它是必需的,因为它指定了需要在 Vector 中检查其出现的元素。
返回值:向量中第一次出现的元素的索引或位置。否则,如果向量中不存在该元素,则返回-1 。返回值是整数类型。
示例 1:
Java
// Java Program to illustrate indexOf() Method
// of Vector class
// Importing required classes
import java.util.*;
// Main class
// VectorDemo
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty Vector by creating object of
// Vector class of string type
Vector vec_tor = new Vector();
// Adding elements in the Vector
// using add() method
vec_tor.add("Geeks");
vec_tor.add("for");
vec_tor.add("Geeks");
vec_tor.add("10");
vec_tor.add("20");
// Print and display all elements in above vector
// object
System.out.println("Vector: " + vec_tor);
// Print commands where we are returning the 1st
// position of element in vector object using
// indexOf() method
System.out.println(
"The first occurrence of Geeks is at index:"
+ vec_tor.indexOf("Geeks"));
System.out.println(
"The first occurrence of 10 is at index: "
+ vec_tor.indexOf("10"));
}
}
Java
// Java Program to illustrate indexOf() Method
// of Vector class
// Importing required classes
import java.util.*;
// Main class
// VectorDemo
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty Vector
Vector vec_tor = new Vector();
// Adding elements in the Vector
// using add() method
vec_tor.add(1);
vec_tor.add(2);
vec_tor.add(3);
vec_tor.add(1);
vec_tor.add(5);
// Print and display all elements of vector object
System.out.println("Vector: " + vec_tor);
// Returning the 1st position of an element
// using indexOf() method
// Print and display commands
System.out.println("The first occurrence of 1 is at index : "+ vec_tor.indexOf(1));
System.out.println("The first occurrence of 3 is at index : "+ vec_tor.indexOf(7));
}
}
输出:
Vector: [Geeks, for, Geeks, 10, 20]
The first occurrence of Geeks is at index:0
The first occurrence of 10 is at index: 3
输出说明:这里我们在向量中插入了元素,其中很少有重复的元素。在上面的例子中,“Geeks”是 Vector 类中唯一被重复的元素,所以我们返回第一个出现在索引处的元素,所以我们返回“0”,而如果元素没有被重复,那么它的索引将是回来。
示例 2:
Java
// Java Program to illustrate indexOf() Method
// of Vector class
// Importing required classes
import java.util.*;
// Main class
// VectorDemo
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty Vector
Vector vec_tor = new Vector();
// Adding elements in the Vector
// using add() method
vec_tor.add(1);
vec_tor.add(2);
vec_tor.add(3);
vec_tor.add(1);
vec_tor.add(5);
// Print and display all elements of vector object
System.out.println("Vector: " + vec_tor);
// Returning the 1st position of an element
// using indexOf() method
// Print and display commands
System.out.println("The first occurrence of 1 is at index : "+ vec_tor.indexOf(1));
System.out.println("The first occurrence of 3 is at index : "+ vec_tor.indexOf(7));
}
}
输出
Vector: [1, 2, 3, 1, 5]
The first occurrence of 1 is at index : 0
The first occurrence of 3 is at index : -1
输出说明:正如我们在上面所做的那样,我们在字符串的情况下采用整数,其中我们唯一不同的是绕过一个不存在的元素,然后将返回“-1”,因为在其中不存在任何负索引Java所以我们通常分配-1。