📜  在Java中使用索引搜索向量中的元素

📅  最后修改于: 2022-05-13 01:55:44.534000             🧑  作者: Mango

在Java中使用索引搜索向量中的元素

Vector 实现了一个动态数组,这意味着它可以根据需要增长或缩小。像数组一样,它包含可以使用整数索引访问的组件。向量的元素 可以使用不同方法的索引进行搜索。假设如果 Vector 中不存在该元素,则该方法将返回 -1。

方法一:(使用indexOf(Object o)

宣言

public int indexOf(Object o)

句法:

Vector.indexOf(Object element)

参数:此方法接受 Vector 类型的强制参数元素。它指定了需要在 Vector 中检查其出现的元素

返回值:此方法返回元素在向量中第一次出现的索引或位置。否则,如果该元素不存在于向量中,则返回-1 。返回值是整数类型。

Java
// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Geeks For Geeks");
  
        // Searching The Index Of Element
        System.out.println(
            "The index of element Geeks For Geeks in Vector is: "
            + details.indexOf("Geeks For Geeks"));
        System.out.println(
            "The index of element 2100 in Vector is: "
            + details.indexOf(2100));
    }
}


Java
// Searching Elements in Vector
// Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");        
        details.add("Geeks For Geeks");
  
        // print an element using it's index
        System.out.println(
            "The index of element Geeks For Geeks in Vector is: "
            + details.lastIndexOf("Geeks For Geeks"));
    }
}


Java
// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Bob");
        details.add("Geeks For Geeks");
  
        System.out.println(
            "The index of element Bob in Vector is: "
            + details.indexOf("Bob", 3));
    }
}


Java
// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // create a instance of vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Bob");
        details.add("Geeks For Geeks");
  
        System.out.println(
            "The index of element Bob in Vector is: "
            + details.lastIndexOf("Bob", 4));
    }
}


输出
The index of element Geeks For Geeks in Vector is: 0
The index of element 2100 in Vector is: -1

方法 2:(使用lastIndexOf(Object o)

宣言

public int lastIndexOf(Object o)

句法:

Vector.lastIndexOf(Object element)

参数:参数元素的类型为 Vector。它指的是需要检查最后一次出现的元素。

返回值:该方法返回元素在 Vector 中最后一次出现的位置。如果该元素不存在于 Vector 中,则该方法返回 -1。返回值是整数类型。

Java

// Searching Elements in Vector
// Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");        
        details.add("Geeks For Geeks");
  
        // print an element using it's index
        System.out.println(
            "The index of element Geeks For Geeks in Vector is: "
            + details.lastIndexOf("Geeks For Geeks"));
    }
}
输出
The index of element Geeks For Geeks in Vector is: 4

方法 3:(使用 indexOf(Object, int) 方法)

宣言

public int indexOf(Object o, int Starting_Index)

句法

Vector.indexOf(Object, int)

参数:

  • 起始索引:它是开始向前搜索元素的索引。
  • o:要搜索的元素。

返回值:在此向量中第一次出现 Object(o) 的索引,从给定的开始索引开始搜索

Java

// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Bob");
        details.add("Geeks For Geeks");
  
        System.out.println(
            "The index of element Bob in Vector is: "
            + details.indexOf("Bob", 3));
    }
}
输出
The index of element Bob in Vector is: 4

方法 4:(使用 lastIndexOf(Object, int) 方法)

宣言

public int lastIndexOf(Object o, int Starting_Index)

句法

Vector.lastIndexOf(Object, int)

参数:

  • 起始索引:它是开始向后搜索元素的索引。
  • o:要搜索的元素。

返回值:此 Vector 中指定元素最后一次出现的索引,从开始索引向后搜索。

Java

// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // create a instance of vector
        Vector details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Bob");
        details.add("Geeks For Geeks");
  
        System.out.println(
            "The index of element Bob in Vector is: "
            + details.lastIndexOf("Bob", 4));
    }
}
输出
The index of element Bob in Vector is: 4