首页  >  文章  >  Java  >  在Java中使用索引在向量中搜索元素

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

王林
王林转载
2023-09-02 14:37:10704浏览

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

向量实现List接口并用于创建动态数组。大小不固定并且可以根据需要增长的数组称为动态数组。向量在使用和功能方面与ArrayList非常相似。

在本文中,我们将学习如何在 Java 中创建向量并通过索引搜索特定元素。我们先讨论一下Vector。

矢量

尽管 Vector 在很多方面与 ArrayList 相似,但也存在一些差异。 Vector 类是同步的,并且它包含几个遗留方法。

同步 - 每当我们对向量执行操作时,它都会限制其同时访问多个线程。如果我们尝试同时通过两个或多个线程访问向量,它将抛出一个名为“ConcurrentModificationException”的异常。与 ArrayList 相比,这使得它的效率较低。

旧类 - 在 Java 1.2 版本发布之前,当集合框架尚未引入时,有一些类描述了该框架类的功能,并用于代替这些类。例如,向量、字典和堆栈。在 JDK 5 中,Java 创建者重新设计了向量并使它们与集合完全兼容。

我们使用以下语法来创建向量。

语法

Vector<TypeOfCollection> nameOfCollection = new Vector<>();

这里,在TypeOfCollection中指定将存储在集合中的元素的数据类型。在nameOfCollection中给出适合您的集合的名称。

通过索引搜索向量中元素的程序

indexOf()

要通过索引搜索 Vector 中的元素,我们可以使用此方法。有两种使用“indexOf()”方法的方法 -

  • indexOf(nameOfObject) - 它接受一个对象作为参数并返回其索引的整数值。如果该对象不属于指定集合,则仅返回-1。

  • indexOf(nameOfObject, index) - 它有两个参数,一个是对象,另一个是索引。它将开始从指定的索引值开始搜索对象。

示例 1

在下面的示例中,我们将定义一个名为“vectlist”的向量,并使用“add()”方法在其中存储一些对象。然后,使用带有单个参数的indexOf()方法,我们将搜索该元素。

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creating a vector 
      Vector< String > vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add("Tutorix");
      vectList.add("Simply");
      vectList.add("Easy");
      vectList.add("Learning");
      vectList.add("Tutorials");
      vectList.add("Point");
      // storing value of index in variable
      int indexValue = vectList.indexOf("Tutorials");
      System.out.println("Index of the specified element in list: " + indexValue);
   }
}

输出

Index of the specified element in list: 4

示例 2

以下示例演示了如果该元素在集合中不可用,则“indexOf()”返回 -1。

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creating a vector 
      Vector< String > vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add("Tutorix");
      vectList.add("Simply");
      vectList.add("Easy");
      vectList.add("Learning");
      vectList.add("Tutorials");
      vectList.add("Point");
      // storing value of index in variable
      int indexValue = vectList.indexOf("Tutorialspoint");
      System.out.println("Index of the specified element in list: " + indexValue);
   }
}

输出

Index of the specified element in list: -1

示例 3

以下示例说明了带有两个参数的“indexOf()”的用法。编译器将从索引 3 开始搜索给定元素。

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creating a vector 
      Vector< String > vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add("Tutorix");
      vectList.add("Simply");
      vectList.add("Easy");
      vectList.add("Learning");
      vectList.add("Tutorials");
      vectList.add("Point");
      vectList.add("Easy");
      vectList.add("Learning");
      // storing value of index in variable
      int indexValue = vectList.indexOf("Easy", 3);
      System.out.println("Index of the specified element in list: " + indexValue);
   }
}

输出

Index of the specified element in list: 6

结论

在本文中,我们讨论了一些示例,这些示例展示了在 Vector 中搜索特定元素时 indexOf() 方法的有用性。我们还了解了 Java 中的 Vector。

以上是在Java中使用索引在向量中搜索元素的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除