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


向量實現了List介面,用於建立動態陣列。大小不固定且可以根據需要增長的陣列稱為動態陣列。向量在使用方法和功能方面與ArrayList非常相似。

在這篇文章中,我們將學習如何在Java中建立一個向量並透過其索引搜尋特定元素。讓我們首先討論向量。

向量

雖然向量在許多方面都類似於ArrayList,但也存在一些差異。Vector類是同步的,並且包含一些遺留方法。

同步 - 每當我們對向量執行操作時,它都會限制多個執行緒同時訪問它。如果我們嘗試同時由兩個或多個執行緒訪問一個向量,則它將丟擲一個名為“ConcurrentModificationException”的異常。這使得它的效率低於ArrayList。

遺留類 - 在Java 1.2版本釋出之前,集合框架尚未引入,那時有一些類描述了這個框架類的特性,並被用作這些類的替代品。例如,Vector、Dictionary和Stack。在JDK 5中,Java建立者重新設計了Vector,並使其完全相容集合。

我們使用以下語法來建立一個向量。

語法

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

這裡,在TypeOfCollection中指定將儲存在集合中的元素的資料型別。在nameOfCollection中為您的集合提供合適的名稱。

透過索引搜尋向量中元素的程式

indexOf()

要透過索引搜尋向量中的元素,我們可以使用此方法。使用“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

結論

在這篇文章中,我們討論了一些示例,這些示例展示了在向量中搜索特定元素時indexOf()方法的有用性。我們還學習了Java中的向量。

更新於:2023年5月15日

瀏覽量:143

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告