首頁  >  文章  >  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刪除