首页 > Java > java教程 > 正文

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

王林
发布: 2023-09-02 14:37:10
转载
1032人浏览过

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

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

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

矢量

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

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

立即学习Java免费学习笔记(深入)”;

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

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

语法

Vector<TypeOfCollection> nameOfCollection = new Vector<>();
登录后复制

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

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

indexOf()

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

纳米搜索
纳米搜索

纳米搜索:360推出的新一代AI搜索引擎

纳米搜索30
查看详情 纳米搜索
  • 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中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号