Java Vector 類
Vector類實現了一個動態數組。和ArrayList和類似,可是二者是不一樣的:java
- Vector是同步訪問的。
- Vector包含了許多傳統的方法,這些方法不屬於集合框架。
Vector主要用在事先不知道數組的大小,或者只是須要一個能夠改變大小的數組的狀況。數組
Vector類支持4種構造方法。框架
第一種構造方法建立一個默認的向量,默認大小爲10:測試
Vector()
第二種構造方法建立指定大小的向量。spa
Vector(int size)
第三種構造方法建立指定大小的向量,而且增量用incr指定. 增量表示向量每次增長的元素數目。code
Vector(int size,int incr)
第四中構造方法建立一個包含集合c元素的向量:對象
Vector(Collection c)
除了從父類繼承的方法外Vector還定義瞭如下方法:blog
序號 | 方法描述 |
---|---|
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