Java Vector 類
Vector 類實現了一個動態數組。和 ArrayList 很相似,但是兩者是不同的:
- Vector 是同步訪問的。
- Vector 包含了許多傳統的方法,這些方法不屬於集合框架。
Vector 主要用在事先不知道數組的大小,或者只是需要一個可以改變大小的數組的情況。
Vector 類支持 4 種構造方法。
第一種構造方法創建一個默認的向量,默認大小為 10:
Vector()
第二種構造方法創建指定大小的向量。
Vector(int size)
第三種構造方法創建指定大小的向量,並且增量用 incr 指定。增量表示向量每次增加的元素數目。
Vector(int size,int incr)
第四種構造方法創建一個包含集合 c 元素的向量:
Vector(Collection c)
除了從父類繼承的方法外 Vector 還定義了以下方法:
序號 | 方法描述 |
---|---|
1 |
void add(int index, Object element) 在此向量的指定位置插入指定的元素。 |
2 |
boolean add(Object o) 將指定元素添加到此向量的末尾。 |
3 |
boolean addAll(Collection c) 將指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的順序添加這些元素。 |
4 |
boolean addAll(int index, Collection c) 在指定位置將指定 Collection 中的所有元素插入到此向量中。 |
5 |
void addElement(Object obj) 將指定的組件添加到此向量的末尾,將其大小增加 1。 |
6 |
int capacity() 返回此向量的當前容量。 |
7 |
void clear() 從此向量中移除所有元素。 |
8 |
Object clone() 返回向量的一個副本。 |
9 |
boolean contains(Object elem) 如果此向量包含指定的元素,則返回 true。 |
10 |
boolean containsAll(Collection c) 如果此向量包含指定 Collection 中的所有元素,則返回 true。 |
11 |
void copyInto(Object[] anArray) 將此向量的組件複製到指定的數組中。 |
12 |
Object elementAt(int index) 返回指定索引處的組件。 |
13 |
Enumeration elements() 返回此向量的組件的枚舉。 |
14 |
void ensureCapacity(int minCapacity) 增加此向量的容量(如有必要),以確保其至少能夠保存最小容量參數指定的組件數。 |
15 |
boolean equals(Object o) 比較指定對象與此向量的相等性。 |
16 |
Object firstElement() 返回此向量的第一個組件(位於索引 0) 處的項)。 |
17 |
Object get(int index) 返回向量中指定位置的元素。 |
18 |
int hashCode() 返回此向量的哈希碼值。 |
19 |
int indexOf(Object elem) 返回此向量中第一次出現的指定元素的索引,如果此向量不包含該元素,則返回 -1。 |
20 |
int indexOf(Object elem, int index) 返回此向量中第一次出現的指定元素的索引,從 index 處正向搜索,如果未找到該元素,則返回 -1。 |
21 |
void insertElementAt(Object obj, int index) 將指定對象作為此向量中的組件插入到指定的 index 處。 |
22 |
boolean isEmpty() 測試此向量是否不包含組件。 |
23 |
Object lastElement() 返回此向量的最後一個組件。 |
24 |
int lastIndexOf(Object elem) 返回此向量中最後一次出現的指定元素的索引;如果此向量不包含該元素,則返回 -1。 |
25 |
int lastIndexOf(Object elem, int index) 返回此向量中最後一次出現的指定元素的索引,從 index 處逆向搜索,如果未找到該元素,則返回 -1。 |
26 |
Object remove(int index) 移除此向量中指定位置的元素。 |
27 |
boolean remove(Object o) 移除此向量中指定元素的第一個匹配項,如果向量不包含該元素,則元素保持不變。 |
28 |
boolean removeAll(Collection c) 從此向量中移除包含在指定 Collection 中的所有元素。 |
29 |
void removeAllElements() 從此向量中移除全部組件,並將其大小設置為零。 |
30 |
boolean removeElement(Object obj) 從此向量中移除變數的第一個(索引最小的)匹配項。 |
31 |
void removeElementAt(int index) 刪除指定索引處的組件。 |
32 |
protected void removeRange(int fromIndex, int toIndex) 從此 List 中移除其索引位於 fromIndex(包括)與 toIndex(不包括)之間的所有元素。 |
33 |
boolean retainAll(Collection c) 在此向量中僅保留包含在指定 Collection 中的元素。 |
34 |
Object set(int index, Object element) 用指定的元素替換此向量中指定位置處的元素。 |
35 |
void setElementAt(Object obj, int index) 將此向量指定 index 處的組件設置為指定的對象。 |
36 |
void setSize(int newSize) 設置此向量的大小。 |
37 |
int size() 返回此向量中的組件數。 |
38 |
List subList(int fromIndex, int toIndex) 返回此 List 的部分視圖,元素範圍為從 fromIndex(包括)到 toIndex(不包括)。 |
39 |
Object[] toArray() 返回一個數組,包含此向量中以恰當順序存放的所有元素。 |
40 |
Object[] toArray(Object[] a) 返回一個數組,包含此向量中以恰當順序存放的所有元素;返回數組的運行時類型為指定數組的類型。 |
41 |
String toString() 返回此向量的字串表示形式,其中包含每個元素的 String 表示形式。 |
42 |
void trimToSize() 對此向量的容量進行微調,使其等於向量的當前大小。 |
實例
下麵的程式說明這個集合所支持的幾種方法:
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