Java Vector 類



介紹

Vector 實現動態陣列。它類似於 ArrayList,但有兩個區別:

  • Vector 是同步的。

  • Vector 包含許多集合框架中不包含的遺留方法。

如果您事先不知道陣列的大小,或者只需要一個在程式生命週期內可以改變大小的陣列,那麼 Vector 將非常有用。

java.util.Vector 類實現可增長的物件陣列。類似於陣列,它包含可以使用整數索引訪問的元件。以下是關於 Vector 的重要幾點:

  • Vector 的大小可以根據需要增長或縮小以適應新增和刪除專案。

  • 每個向量都嘗試透過維護容量容量增量來最佳化儲存管理。

  • 從 Java 2 平臺 v1.2 開始,此類經過改造以實現 List 介面。

  • 與新的集合實現不同,Vector 是同步的。

  • 此類是 Java 集合框架的成員。

類宣告

以下是java.util.Vector類的宣告:

public class Vector<E>
   extends AbstractList<E>
   implements List<E>, RandomAccess, Cloneable, Serializable

這裡<E>代表一個元素,可以是任何類。例如,如果您正在構建一個整數的陣列列表,則應按如下方式初始化它:

ArrayList<Integer> list = new ArrayList<Integer>();  

類建構函式

序號 建構函式和說明
1

Vector()

此建構函式用於建立一個空向量,以便其內部資料陣列大小為 10,其標準容量增量為零。

2

Vector(Collection<? extends E> c)

此建構函式用於建立一個包含指定集合元素的向量,其順序與集合的迭代器返回的順序相同。

3

Vector(int initialCapacity)

此建構函式用於建立一個具有指定初始容量且容量增量等於零的空向量。

4

Vector(int initialCapacity, int capacityIncrement)

此建構函式用於建立一個具有指定初始容量和容量增量的空向量。

類方法

序號 方法和說明
1 boolean add(E e)

此方法將指定的元素新增到此向量的末尾。

2 boolean addAll(Collection<? extends E> c)

此方法將指定集合中的所有元素新增到此向量的末尾。

3 void addElement(E obj)

此方法將指定的元件新增到此向量的末尾,將其大小增加一。

4 int capacity()

此方法返回此向量的當前容量。

5 void clear()

此方法從此向量中刪除所有元素。

6 Vector clone()

此方法返回此向量的克隆。

7 boolean contains(Object o)

如果此向量包含指定的元素,則此方法返回 true。

8 boolean containsAll(Collection<?> c)

如果此 Vector 包含指定集合中的所有元素,則此方法返回 true。

9 void copyInto(Object[ ] anArray)

此方法將此向量的元件複製到指定的陣列中。

10 E elementAt(int index)

此方法返回指定索引處的元件。

11 Enumeration<E> elements()

此方法返回此向量的元件的列舉。

12 void ensureCapacity(int minCapacity)

此方法根據需要增加此向量的容量,以確保它至少可以容納最小容量引數指定的元件數量。

13 boolean equals(Object o)

此方法比較指定的 Object 與此 Vector 的相等性。

14 E firstElement()

此方法返回此向量的第一個元件(索引 0 處的專案)。

15 void forEach​(Consumer<? super E> action)

此方法對 Iterable 的每個元素執行給定的操作,直到所有元素都已處理或操作引發異常。

16 E get(int index)

此方法返回此向量中指定位置處的元素。

17 int hashCode()

此方法返回此向量的雜湊碼值。

18 int indexOf(Object o)

此方法返回此向量中指定元素的第一次出現的索引,如果此向量不包含該元素,則返回 -1。

19 void insertElementAt(E obj, int index)

此方法將指定的物件作為元件插入此向量中的指定索引處。

20 boolean isEmpty()

此方法測試此向量是否沒有元件。

21 Iterator<E> iterator()

此方法返回此列表中元素的迭代器,順序正確。

22 E lastElement()

此方法返回向量的最後一個元件。

23 int lastIndexOf​(Object o)

此方法返回此向量中指定元素的最後一次出現的索引,如果此向量不包含該元素,則返回 -1。

24 ListIterator<E> listIterator()

此方法返回此列表中元素的列表迭代器(順序正確)。

25 E remove(int index)

此方法從此向量中刪除指定位置處的元素。

26 boolean removeAll(Collection<?> c)

此方法從此向量中刪除其包含在指定集合中的所有元素。

27 void removeAllElements()

此方法從此向量中刪除所有元件並將大小設定為零。

28 boolean removeElement(Object obj)

此方法從此向量中刪除引數的第一次出現。

29 void removeElementAt(int index)

此方法刪除指定索引處的元件。

30 boolean removeIf​(Predicate<? super E> filter)

刪除滿足給定謂詞的此集合的所有元素。

31 boolean retainAll(Collection<?> c)

此方法僅保留此向量中包含在指定集合中的元素。

32 E set(int index, E element)

此方法將此向量中指定位置處的元素替換為指定的元素。

33 void setElementAt(E obj, int index)

此方法將此向量的指定索引處的元件設定為指定的物件。

34 void setSize(int newSize)

此方法設定此向量的 size。

35 int size()

此方法返回此向量中元件的數量。

36 Spliterator<E> spliterator()

在此列表中的元素上建立一個延遲繫結和快速失敗的 Spliterator。

37 List <E> subList(int fromIndex, int toIndex)

此方法返回此列表從 fromIndex(包含)到 toIndex(不包含)部分的檢視。

38 object[] toArray()

此方法返回一個數組,其中包含此向量中的所有元素,順序正確。

39 String toString()

此方法返回此向量的字串表示形式,其中包含每個元素的字串表示形式。

40 void trimToSize()

此方法將此向量的容量修剪為向量的當前大小。

繼承的方法

此類繼承自以下類的方法:

  • java.util.AbstractMap
  • java.lang.Object
  • java.util.List

新增元素和迭代 Vector 示例

以下程式說明了 Vector 集合支援的幾種方法:

import java.util.*;
public class VectorDemo {

   public static void main(String args[]) {
      // initial size is 3, increment is 2
      Vector v = new Vector(3, 2);
      System.out.println("Initial size: " + v.size());
      System.out.println("Initial capacity: " + v.capacity());
      
      v.addElement(new Integer(1));
      v.addElement(new Integer(2));
      v.addElement(new Integer(3));
      v.addElement(new Integer(4));
      System.out.println("Capacity after four additions: " + v.capacity());

      v.addElement(new Double(5.45));
      System.out.println("Current capacity: " + v.capacity());
      
      v.addElement(new Double(6.08));
      v.addElement(new Integer(7));
      System.out.println("Current capacity: " + v.capacity());
      
      v.addElement(new Float(9.4));
      v.addElement(new Integer(10));
      System.out.println("Current capacity: " + v.capacity());
      
      v.addElement(new Integer(11));
      v.addElement(new Integer(12));
      System.out.println("First element: " + (Integer)v.firstElement());
      System.out.println("Last element: " + (Integer)v.lastElement());
      
      if(v.contains(new Integer(3)))
         System.out.println("Vector contains 3.");
         
      // enumerate the elements in the vector.
      Enumeration vEnum = v.elements();
      System.out.println("\nElements in vector:");
      
      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
}

這將產生以下結果:

輸出

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12
廣告