String[] names = new String[10]; 或 String[] names = {"a","b","c"};int[][] coords = new int[12][12];length。hasMoreElements():是否有下一個nextElement():取得下一個,無則拋 NoSuchElementException位元集合,適合布林旗標群組。方法:set(index)、clear(index)、get(index)、size()、xor(BitSet) 等。
new Vector(20, 5)(初始 20,每次增 5)。java.util。先進後出 (LIFO)。方法:push()、pop()、peek()、empty()、search(Object)。
定義鍵值對應結構。Hashtable 繼承自此。抽象方法:put(k,v)、get(k)、remove(k)、size()、isEmpty()、keys()、elements()。
Dictionary,實作 Serializable,常用於序列化傳輸。Hashtable()、Hashtable(int)、Hashtable(int, float)、Hashtable(Map)。capacity * loadFactor 達臨界值時 rehash(擴容為 2x capacity)。預設 loadFactor=0.75。put(key, value) 會計算 hashCode。三個類別:
LinkedListEntry:節點(值、前驅、後繼)LinkedList:主類別(頭、尾、節點數)LinkedListEnumerator:實作 Enumerationclass LinkedListEntry {
protected Object val = null;
protected LinkedListEntry next = null;
protected LinkedListEntry prev = null;
public LinkedListEntry(Object obj) {
if (obj == null) throw new NullPointerException();
val = obj;
}
}
class LinkedListEnumerator implements Enumeration {
protected LinkedListEntry pos;
public LinkedListEnumerator(LinkedList list) { pos = list.start; }
public boolean hasMoreElements() { return pos != null; }
public Object nextElement() {
if (pos == null) throw new NoSuchElementException();
LinkedListEntry tmp = pos;
pos = pos.next;
return tmp.val;
}
}
欄位:start、end、numElements
public void addElement(Object obj) {
if (obj == null) throw new NullPointerException();
LinkedListEntry newElement = new LinkedListEntry(obj);
numElements++;
if (start == null) {
start = end = newElement;
} else {
end.next = newElement;
newElement.prev = end;
end = newElement;
}
}
insertElementAt (指定節點前插入)
public void insertElementAt(Object obj, Object pos) {
if (obj == null || pos == null) throw new NullPointerException();
LinkedListEntry posEntry = find(pos);
if (posEntry == null) throw new NullPointerException();
LinkedListEntry newElement = new LinkedListEntry(obj);
numElements++;
newElement.next = posEntry;
newElement.prev = posEntry.prev;
if (posEntry == start) start = newElement;
else posEntry.prev.next = newElement;
posEntry.prev = newElement;
}
find (依值查找)
private LinkedListEntry find(Object obj) {
if (isEmpty() || obj == null) return null;
LinkedListEntry tmp = start;
while (tmp != null) {
if (tmp.val == obj) return tmp;
tmp = tmp.next;
}
return null;
}
contains / isEmpty / elements
public boolean contains(Object obj) { return find(obj) != null; }
public boolean isEmpty() { return start == null; }
public Enumeration elements() { return new LinkedListEnumerator(this); }
Java 內建資料結構涵蓋陣列、位元集、動態陣列、堆疊、字典/雜湊表。若不足,可如雙向鏈表示範自行實作。實務上集合框架 (ArrayList, LinkedList, HashMap 等) 已取代早期類別,新專案建議使用 java.util 集合框架。