JAVA類別 - Vector

Java Vector 類別

在JAVA中如果要存儲和建立一組同類型的數據的時候,我們一般都采用陣列來存儲。但是大家知道數組一旦被創建,其長度就固定不變了,所以使用數組的時候需要知道或者說是我們要估算一下數據的規模,以方便我們創建長度適合的陣列。如果我們估計的長度比實際需要的長度大,那則會浪費存儲空間;若比實際長度小,則處理數據時會遇到麻煩,因此,用數據存儲數目不確定的元素那樣是一個不明智的選擇。

所以這時候我們就需要使用java.util包中為我們提供的向量類Vector。Vector對象可以根據需要動態伸縮,類似於ArrayList,但是它可以存儲多個對象,並且可以根據索引值來檢索這些對象。

陣列和Vector的最大區別就是當空間用完以後,Vector會自動增長。同時Vector還提供了額外的方法來增加或者刪除元素,而在陣列中,必須手工完成。

建構方法:

  1. public Vector(); 創建一個空的Vector;

  2. public Vector(int initialcapacity); 創建一個Vector,其初始化大小為initial capacity.

  3. public Vector(int initialcapacity, int capacityIncrement); 創建一個Vector,其初始化大小為initial capacity, 當Vector需要增長時,其增長速度由capacityIncrement決定。

    NOTE:如果增長的速度不指定,那麽Vector會將其空間增加一倍。當Vector很大的時候,這可能導致系統性能下降以及其他問題,建議設置具體的增長速度。

  4. Vector(Collection c); 創建一個包含集合c的元素的向量

常用方法

Vector 類提供的訪問方法支持類似數組運算和與Vector 大小相關的運算。類似數組的運算允許向量中增加,刪除和插入元素。它們也允許測試矢量的內容和檢索指定的元素,與大小相關的運算允許判定字節大小和矢量中元素不數目。

addElement(Object obj) 把組件加到向量尾部,同時大小加1,向量容量比以前大1

insertElementAt(Object obj, int index) 把組件加到所定索引處,此後的內容向後移動1 個單位  

setElementAt(Object obj, int index)把組件加到所定索引處,此處的內容被代替。

removeElement(Object obj) 把向量中含有本組件內容移走。

removeAllElements()把向量中所有組件移走,向量大小為0。  

void add(int index, Object element) 插入在此向量的指定位置插入指定的元素。

boolean add(Object o) 將指定的元素添加到此向量的末尾。

boolean addAll(Collection c)所有追加在指定集合的元素添加到此向量的末尾,因為它們是由指定集合的迭代器返回的順序。

boolean addAll(int index, Collection c) 插入所有在指定Collection中的元素到此向量的指定位置。

int capacity() 返回此向量的當前容量。

void clear() 移除此向量中的所有元素。

Object clone() 返回此向量的一個副本。

boolean contains(Object elem) 如果測試指定的對象在此向量的組件。

boolean containsAll(Collection c) 返回true如果此向量包含指定Collection中的所有元素。

void copyInto(Object[] anArray) 將此向量的組件複製到指定的數組中。

Object elementAt(int index) 返回組件的指定索引處。

Enumeration elements() 返回此向量的組件的枚舉。

void ensureCapacity(int minCapacity) 增加此向量的容量,如果需要,以確保它能夠保存最小容量參數指定的組件數量最少。

boolean equals(Object o) 比較指定對象與此向量的相等性。

Object firstElement() 返回此向量的第一個組件(位於索引0處的項)

Object get(int index)返回此向量中指定位置的元素。

int hashCode() 返回此向量的哈希碼值。

int indexOf(Object elem). 搜索給定參數,用equals方法測試相等的第一次出現元素。

int indexOf(Object elem, int index) 搜索給定參數,在開始搜索索引,並測試使用equals方法相等的第一次出現

void insertElementAt(Object obj, int index) 指定對象插入在此向量中指定索引處的組件。

boolean isEmpty() 如果測試此向量是否不包含組件。

Object lastElement() 返回此向量的最後一個組件。

int lastIndexOf(Object elem) 返回此向量的指定對象的最後一個匹配項的索引。

int lastIndexOf(Object elem, int index) 向後搜索指定的對象,從指定的索引開始,並返回它的下標。

Object remove(int index) 移除元素在向量中指定位置。

boolean remove(Object o)** 在移除此向量中指定元素的第一個匹配,如果向量不包含該元素,它是不變的

boolean removeAll(Collection c) 移除此向量的所有元素包含在指定Collection。

void removeAllElements( 移除全部組件從這個載體,並將其大小設置為零。

boolean removeElement(Object obj)刪除第一個(索引最小的)匹配從這個向量的參數。

void removeElementAt(int index) removeElementAt(int index)

protected void removeRange(int fromIndex, int toIndex)從這個列表中刪除所有索引為fromIndex(包括)和的toIndex,獨占的元素。

boolean retainAll(Collection c) 保留包含在指定Collection在此向量中僅元素。

Object set(int index, Object element) 替換元素在與指定元素在此向量的指定位置。

void setElementAt(Object obj, int index) 設置在向量的指定索引處是指定的對象。

void setSize(int newSize)設置此向量的大小。

int size()返回此向量中的組件的數量。

List subList(int fromIndex, int toIndex) 返回fromIndex(包括)和toIndex,獨享這之間List部分視圖。

Object[] toArray() 返回包含所有在此向量中以正確的順序元素的數組。

Object[] toArray(Object[] a) 返回包含所有在此向量中以正確的順序元素的數組;返回數組的運行時類型是指定數組的。

String toString() 返回此向量的字符串表示形式,其中包含每個元素的String表示。

void trimToSize() 這個微調,向量是向量的當前大小的容量。

舉例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;

public class VectorDemo {

public static void main(String args[]) {
// initial capacity 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("
Elements in vector:");

while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}

結果:

1
2
3
4
5
6
7
8
9
10
11
12
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