這次我們接著上篇文章來看下java的資料結構之向量(Vector)&棧(Stack)。首先是向量(Vector),向量(Vector)類和傳統數組非常相似,但是Vector的大小能根據需要動態變化。和數組一樣,Vector對象的元素也能通過索引訪問。使用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: 0Initial capacity: 3Capacity after four additions: 5Current capacity: 5Current capacity: 7Current capacity: 9First element: 1Last element: 12Vector contains 3.Elements in vector:1 2 3 4 5.45 6.08 7 9.4 10 11 12
然後我們再來看棧(Stack)。棧(Stack)實現了一個後進先出(LIFO)的資料結構。你可以把棧理解為對象的垂直分布的棧,當你添加一個新元素時,就將新元素放在其他元素的頂部。當你從棧中取元素的時候,就從棧頂取一個元素。換句話說,最後進棧的元素最先被取出。棧是Vector的一個子類,它實現了一個標準的後進先出的棧。堆棧只定義了預設建構函式,用來建立一個空棧。 堆棧除了包括由Vector定義的所有方法,也定義了自己的一些方法:
Stack()
除了由Vector定義的所有方法,自己也定義了一些方法:
| 序號 |
方法描述 |
| 1 |
boolean empty() 測試堆棧是否為空白。 |
| 2 |
Object peek( ) 查看堆棧頂部的對象,但不從堆棧中移除它。 |
| 3 |
Object pop( ) 移除堆棧頂部的對象,並作為此函數的值返回該對象。 |
| 4 |
Object push(Object element) 把項壓入堆棧頂部。 |
| 5 |
int search(Object element) 返回對象在堆棧中的位置,以 1 為基數。 |
下面的程式說明這個集合所支援的幾種方法:
import java.util.*;
public class StackDemo {
static void showpush(Stack<Integer> st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack<Integer> st) {
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[]) {
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
運行結果為:
stack: [ ]push(42)stack: [42]push(66)stack: [42, 66]push(99)stack: [42, 66, 99]pop -> 99stack: [42, 66]pop -> 66stack: [42]pop -> 42stack: [ ]pop -> empty stack
好啦,這次就到這裡了。如果感覺不錯的話,請多多點贊支援哦。。。