List是一種特殊的集合形式,該類型允許集合內元素有序、可重複。
在List模塊的學習中,可以主要關注6個類(或接口),主要繼承關係如下:
這6個就是要基本掌握的所有List所有內容。
一、List
List是一個接口(interface),繼承Collection接口。
List特點:
1>是一個有序的、可重複的集合,集合中每一個元素都有對應的順序索引。
2>允許加入重複元素是應爲可以通過索引來訪問指定位置的元素。
3>默認按照元素的添加順序增加元素的索引。
1.1 添加元素
1>public void add(int index, E element)
在列表的指定位置插入指定元素。
2>public boolean add(E object)
向列表的尾部添加指定的元素,添加成功則返回true。
3>public boolean addAll(int index, Collection<? extends E> collection)
將指定 collection 中的所有元素都插入到列表中的指定位置,添加成功則返回true。
4>public boolean addAll(Collection<? extends E> collection)
添加指定 collection 中的所有元素到此列表的結尾,添加成功則返回true。
1.2 刪除元素
1>public E remove(int location)
移除並返回列表中指定位置的元素。
2>public boolean remove(Object object)
從此列表中移除第一次出現的指定元素。更確切地講,移除滿足 (object == null ? get(i)==null : object.equals(get(i))) 的最低索引 i 的元素,移除成功則返回true。
3>public boolean removeAll(Collection<?> collection)
從列表中移除指定 collection 中包含的其所有元素。
4>public void clear()
從列表中移除所有元素。
1.3 遍歷元素
1>public Iterator iterator()
返回此列表元素的列表迭代器.
2>public ListIterator listIterator()
返回此列表元素的列表迭代器。
3>public ListIterator listIterator(int index)
返回列表中元素的列表迭代器,從列表的指定位置開始。
1.4 判斷包含關係
1>public boolean contains(Object object)
如果列表包含指定的元素,則返回 true。更確切地講,當且僅當列表包含滿足(object == null ? object == null : object.equals(e)) 的元素 e 時才返回 true。
2>public boolean containsAll(Collection<?> collection)
如果列表包含指定 collection 的所有元素,則返回 true。
1.5 檢索元素
1>public int indexOf(Object object)
返回此列表中第一次出現的指定元素的索引;如果此列表不包含該元素,則返回-1。更確切地講,返回滿足 (object==null ? get(i) == null : object.equals(get(i))) 的最低索引 i;如果沒有這樣的索引,則返回 -1。
2>public int lastIndexOf(Object object)
返回此列表中最後出現的指定元素的索引;如果列表不包含此元素,則返回 -1。更確切地講,返回滿足 (object == null ? get(i)==null : object.equals(get(i))) 的最高索引 i;如果沒有這樣的索引,則返回 -1。
1.6 其他方法
1>public boolean equals(Object object)
比較指定的對象與列表是否相等。當且僅當指定的對象也是一個列表、兩個列表
有相同的大小,並且兩個列表中的所有相應的元素對相等 時才返回 true( 如果
(e1 == null ? e2 == null :e1.equals(e2)),則兩個元素 e1 和 e2 是相等 的)。
2>public E get(int index)
返回列表中指定位置的元素。
3>public int hashCode()
返回列表的哈希碼值。
4>public boolean retainAll(Collection<?> collection)
僅在列表中保留指定 collection 中所包含的元素,即是否有交集,有交集則返回true。
5>public E set(int location, E object)
用指定元素替換列表中指定位置的元素。
6>public int size()
返回列表中的元素數。如果列表包含多於 Integer.MAX_VALUE 個元素,則返回 Integer.MAX_VALUE。
7>public boolean isEmpty()
如果列表不包含元素,則返回 true。
8>public List subList(int start, int end)
返回列表中指定的 start(包括 )和 end(不包括)之間的部分視圖。
9>public Object[ ] toArray()
返回按適當順序包含列表中的所有元素的數組。
10>public T[ ] toArray(T[ ] array)
返回指定類型的、包含該collection元素的數組。
二、AbstractList
AbstractList是一個抽象類,繼承AbstractCollection類,實現List接口。
AbstractList實現了 List 的一些位置相關操作(比如 get,set,add,remove),是第一個實現隨機訪問方法的集合類,但不支持添加和替換。
2.1 添加元素
1>public void add(int location, E object)
在列表的指定位置插入指定元素。
2>public boolean add(E object)
在列表末尾插入指定元素。
3>public boolean addAll(int location, Collection<? extends E> collection)
將指定 collection 中的所有元素都插入到列表中的指定位置。
2.2 刪除元素
1>public E remove(int location)
移除列表中指定位置的元素。
2>protected void removeRange(int start, int end)
從此列表中移除索引在 start(包括)和 end(不包括)之間的所有元素(如果 start == end,則此操作無效)。
3>public void clear()
從此列表中移除所有元素。
2.3 遍歷元素
1>public Iterator iterator()
返回一個迭代器。
2>public ListIterator listIterator()
返回此列表元素的列表迭代器。
3>public ListIterator listIterator(int location)
返回列表中元素的列表迭代器。
2.4 檢索元素
1>public int indexOf(Object object)
返回此列表中第一次出現的指定元素的索引。如果此列表不包含該元素,則返回 -1。更確切地講,返回滿足 (o==null ? get(i) == null : o.equals(get(i))) 的最低索引 i;如果沒有這樣的索引,則返回 -1。
2>public int lastIndexOf(Object object)
返回此列表中最後出現的指定元素的索引。如果列表不包含此元素,則返回 -1。更確切地講,返回滿足 (object == null ? get(i) == null : object.equals(get(i))) 的最高索引 i;如果沒有這樣的索引,則返回 -1。
2.5 其他方法
1>public boolean equals(Object object)
當且僅當指定的對象也是一個列表,兩個列表具有相同的大小,而且兩個列表中所有相應的元素對都相等 時,才返回 true(如果 (e1 == null ? e2 == null : e1.equals(e2)),則元素 e1 和 e2 相等)。
2>public abstract E get(int location)
返回列表中指定位置的元素。
3>public int hashCode()
返回此列表的哈希碼值。
4>public E set(int location, E object)
用指定元素替換列表中指定位置的元素。
5>public List subList(int start, int end)
返回列表中指定的 start(包括 )和 end(不包括)之間的部分視圖(如果 start和 end相等,則返回的列表爲空)。
2.6 特殊方法
1、add相關方法
AbstractList中並沒有實現add方法,所以需要先實現add方法,才能調用add相關的方法。public void add(int location, E object)方法具體代碼如下:
public void add(int location, E object) {
throw new UnsupportedOperationException();
}
2、remove相關方法
與add相關方法相似,AbstractList中也沒有實現remove方法,所以需要先實現remove方法,才能調用remove相關的方法。public E remove(int location)方法具體代碼如下:
public E remove(int location) {
throw new UnsupportedOperationException();
}
3、get方法
AbstractList是抽象類,get就是類中的抽象方法,需要子類去實現,抽象方法爲:
public abstract E get(int location)
4、set方法
AbstractList中,set方法其實也是需要子類去實現的,public E set(int location, E object)方法具體代碼如下:
public E set(int location, E object) {
throw new UnsupportedOperationException();
}
三、LinkedList
此類實現List接口,提供了List 接口的鏈表實現。
LinkedList特點:
1>LinkedList 是一個繼承於AbstractSequentialList的雙向鏈表。它也可以被當作堆棧、隊列或雙端隊列進行操作。
2>LinkedList 實現 List 接口,能對它進行隊列操作。
3>LinkedList 實現 Deque 接口,即能將LinkedList當作雙端隊列使用。
4>LinkedList 實現了Cloneable接口,即覆蓋了函數clone(),能克隆。
5>LinkedList 實現java.io.Serializable接口,這意味着LinkedList支持序列化,能通過序列化去傳輸。
6>LinkedList 是非同步的。
3.1 構造方法
1>public LinkedList()
構造一個空鏈表。
2>public LinkedList(Collection<? extends E> collection)
構造一個包含指定 collection 中的元素的列表。
3.2 添加元素
1>public void add(int location, E object)
在此列表中指定的位置插入指定的元素。
2>public boolean add(E object)
在鏈表末尾添加元素。
3>public boolean addAll(int location, Collection<? extends E> collection)
將集合中的元素插入鏈表。
4>public boolean addAll(Collection<? extends E> collection)
將集合中的元素插入鏈表末尾。
5>public void addFirst(E object)
在鏈表頭部添加元素。
6>public void addLast(E object)
在鏈表尾部添加元素。
7>public boolean offerFirst(E e)
在列表的開頭插入指定的元素。
8>public boolean offerLast(E e)
在此列表末尾插入指定的元素。
9>public boolean offer(E o)
將指定元素添加到此列表的末尾。
10>public void push(E e)
將元素推入此列表所表示的堆棧。
3.3 刪除元素
1>public E remove(int location)
移除指定位置元素。
2>public boolean remove(Object object)
移除第一個出現的指定元素。
3>public E removeFirst()
移除第一個元素 。
4>public E removeLast()
移除最後一個元素。
5>public E pop()
從此列表所表示的堆棧處彈出一個元素。
6>public boolean removeFirstOccurrence(Object o)
從此列表中移除第一次出現的指定元素。
7>public boolean removeLastOccurrence(Object o)
從此列表中移除最後一次出現的指定元素。
8>public void clear()
清空鏈表。
3.4 獲取元素
1>public E get(int location)
獲取對應位置的元素。
2>public E getFirst()
獲取第一個元素。
3>public E getLast()
獲取最後一個元素。
4>public int indexOf(Object object)
檢索元素位置。
5>public int lastIndexOf(Object object)
返回元素最後一次出現的位置。
6>public E peekFirst()
獲取但不移除此列表的第一個元素;如果此列表爲空,則返回 null。
7>public E peekLast()
獲取但不移除此列表的最後一個元素。
8>public E pollFirst()
獲取並移除此列表的第一個元素。
9>public E pollLast()
獲取並移除此列表的最後一個元素。
10>public E set(int location, E object)
將此列表中指定位置的元素替換爲指定的元素。
11>public E poll()
獲取並移除此列表的頭(第一個元素)。
12>public E remove()
獲取並移除此列表的頭(第一個元素)。
13>public E peek()
獲取但不移除此列表的頭(第一個元素)。
14>public E element()
獲取但不移除此列表的頭(第一個元素)。
3.5 其他方法
1>public Object clone()
克隆鏈表。
2>public boolean contains(Object object)
判斷鏈表中是否包含某個元素。
3>public ListIterator listIterator(int location)
返回從指定位置開始的列表迭代器 。
4>public Iterator descendingIterator()
返回以逆向順序在此雙端隊列的元素上進行迭代的迭代器。
5>public int size()
鏈表元素個數。
6>public Object [ ] toArray()
返回以適當順序(從第一個元素到最後一個元素)包含此列表中所有元素的數組。
7>public T[] toArray(T[ ] contents)
返回包含當前鏈表元素、指定數組形式的數組。
3.6 特殊方法
1、public void add(int location, E object)
該方法的作用是在指定位置插入一個元素,源碼如下:
public void add(int location, E object) {
if (location >= 0 && location <= size) {
Link<E> link = voidLink;
if (location < (size / 2)) {
for (int i = 0; i <= location; i++) {
link = link.next;
}
} else {
for (int i = size; i > location; i--) {
link = link.previous;
}
}
Link<E> previous = link.previous;
Link<E> newLink = new Link<E>(object, previous, link);
previous.next = newLink;
link.previous = newLink;
size++;
modCount++;
} else {
throw new IndexOutOfBoundsException();
}
}
從上述代碼可以看出,該方法的實現是:先檢驗location參數的合法性,不合法則拋出IndexOutOfBoundsException。合法的情況下,再檢查該位置是在List的前半部還是在後半部,如果在前半部,則從前向後遍歷,如果在後半部,則從後向前查找location位置。找到對應的位置後,用object創建一個新的節點,插入到List中,List元素個數size++。此處有意思的是modCount變量,該變量是用在fail-fast機制中的,由於LinkedList是線程不安全的,在多個線程中遍歷List中的元素時,如果有某個線程修改了元素,就有可能產生不同的結果,該modCount變量相當於一個動態計數器,如果遍歷時的元素個數與modCount不一致時,直接拋出異常,從而避免多個線程遍歷產生不同的結果。
2、public boolean addAll(int location, Collection<? extends E> collection)
該方法的作用是在指定位置插入一個collection中的所有元素,其實現和上個方法相似,多加入了對collection元素個數的校驗。
3、public E remove(int location)
其實remove的方法也很多,實現方式也與add方法相似,如刪除固定的位置的一個元素:
public E remove(int location) {
if (location >= 0 && location < size) {
Link<E> link = voidLink;
if (location < (size / 2)) {
for (int i = 0; i <= location; i++) {
link = link.next;
}
} else {
for (int i = size; i > location; i--) {
link = link.previous;
}
}
Link<E> previous = link.previous;
Link<E> next = link.next;
previous.next = next;
next.previous = previous;
size--;
modCount++;
return link.data;
}
throw new IndexOutOfBoundsException();
}
3.7 常用遍歷方法
LinkedList常用的遍歷方法有4種:Iterator遍歷、ListIterator遍歷、普通for循環和foreach循環。
1>Iterator遍歷,示例代碼如下:
Iterator<String> iterator=testLinkedList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
1>ListIterator遍歷,示例代碼如下:
ListIterator<String> listIterator=testLinkedList.listIterator();
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
3>普通for循環,示例代碼如下:
for(int i=0;i<testLinkedList.size();i++){
System.out.println(testLinkedList.get(i));
}
4>foreach循環,示例代碼如下:
for(String str:testLinkedList){
System.out.println(str);
}
四、ArrayList
ArrayList繼承AbstractList,可以理解爲實現了List接口的動態數組。
ArrayList不是線程安全的,只能用在單線程環境下,多線程環境下可以考慮用Collections.synchronizedList(List l)函數返回一個線程安全的ArrayList類。
4.1 構造方法
1>public ArrayList(int capacity)
構造一個具有指定初始容量的空列表。
2>public ArrayList( )
構造一個初始容量爲 10 的空列表。
3>public ArrayList(Collection<? extends E> collection)
構造一個包含指定 collection 的元素的列表。
4.2 添加元素
1>public boolean add(E object)
將指定的元素添加到此列表的尾部。
2>public void add(int index, E object)
將指定的元素插入此列表中的指定位置。
3>public boolean addAll(Collection<? extends E> collection)
按照指定 collection 的迭代器所返回的元素順序,將該 collection 中的所有元素添加到此列表的尾部。
4>public boolean addAll(int index, Collection<? extends E> collection)
在指定的位置追加collection中的元素。
4.3 刪除元素
1>public E remove(int index)
移除此列表中指定位置上的元素。
2>public boolean remove(Object object)
移除此列表中首次出現的指定元素(如果存在)。如果列表不包含此元素,則列表不做改動。更確切地講,移除滿足 (o == null ? get(i) == null : o.equals(get(i))) 的最低索引的元素(如果存在此類元素)。
3>protected void removeRange(int fromIndex, int toIndex)
移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之間的所有元素。
4>public void clear()
移除此列表中的所有元素。
4.4 其他方法
1>public Object clone()
返回此 ArrayList 實例的淺表副本。
2>public void ensureCapacity(int minimumCapacity)
如有必要,增加此 ArrayList 實例的容量,以確保它至少能夠容納最小容量參數所指定的元素數。
3>public E get(int index)
返回此列表中指定位置上的元素。
4>public int size()
數組中元素數量。
5>public boolean isEmpty()
數組中元素個數是否爲0。
6>public boolean contains(Object object)
如果此列表中包含指定的元素,則返回 true。更確切地講,當且僅當此列表包含至少一個滿足 (object == null ? e == null : object.equals(e)) 的元素 e 時,則返回 true。
7>public int indexOf(Object object)
返回此列表中首次出現的指定元素的索引,或如果此列表不包含元素,則返回 -1。更確切地講,返回滿足 (object==null ? get(i)==null : object.equals(get(i))) 的最低索引 i ,如果不存在此類索引,則返回 -1。
8>public int lastIndexOf(Object object)
返回此列表中最後一次出現的指定元素的索引,或如果此列表不包含索引,則返回 -1。更確切地講,返回滿足 (object == null ? get(i) == null : object.equals(get(i))) 的最高索引 i,如果不存在此類索引,則返回 -1。
9>public E set(int index, E object)
用指定的元素替代此列表中指定位置上的元素。
10>public Object[ ] toArray()
返回包含此列表中所有元素的數組。
11>public T[ ] toArray(T[] contents)
返回指定數組形式、包含該List的數組。
12>public void trimToSize()
將此 ArrayList 實例的容量調整爲列表的當前大小。
13>public Iterator iterator()
返回一個迭代器。
14>public int hashCode()
返回哈希值。
15>public boolean equals(Object o)
List比較。
4.5 特殊方法
1、public ArrayList(Collection<? extends E> collection)
ArrayList有三種構造方法,空構造、指定容量構造和這種指定collection構造。這種構造方法的實現是:先將collection轉換爲數組,然後再將轉換後的數組拷貝到指定數組中。
2、public void add(int index, E object)
該方法的作用是在指定位置插入一個元素,示例代碼:
public void add(int index, E object) {
Object[] a = array;
int s = size;
if (index > s || index < 0) {
throwIndexOutOfBoundsException(index, s);
}
if (s < a.length) {
System.arraycopy(a, index, a, index + 1, s - index);
} else {
// assert s == a.length;
Object[] newArray = new Object[newCapacity(s)];
System.arraycopy(a, 0, newArray, 0, index);
System.arraycopy(a, index, newArray, index + 1, s - index);
array = a = newArray;
}
a[index] = object;
size = s + 1;
modCount++;
}
從上述代碼可以看出,實現的邏輯爲:先檢測index參數的合法性,然後檢查數組是否完全填滿了元素(此處表明ArrayList是可以存儲空元素的)。如果ArrayList未填滿,直接插入指定元素,否則先擴容一個單位,再插入指定元素。
3、private static int newCapacity(int currentCapacity)
該方法的作用是對數組進行擴容,代碼如下:
private static int newCapacity(int currentCapacity) {
int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT : currentCapacity >> 1);
return currentCapacity + increment;
}
MIN_CAPACITY_INCREMENT是一個常量,值爲12,。從上面可以看出,數組擴容需要先計算出一個增量increment,當數組原容量小於6時,增量爲12,否則增量爲原始容量的一半。
4、public void ensureCapacity(int minimumCapacity)
該方法的作用是確保ArrayList可以容納minimumCapacity的元素,代碼示例如下:
public void ensureCapacity(int minimumCapacity) {
Object[] a = array;
if (a.length < minimumCapacity) {
Object[] newArray = new Object[minimumCapacity];
System.arraycopy(a, 0, newArray, 0, size);
array = newArray;
modCount++;
}
}
從上面代碼可以看出,實現爲:判斷當前ArrayList容量是否小於minimumCapacity,如果小於,則創建一個容量爲minimumCapacity的數組,然後再把原來的元素拷貝過去,modCount自增1。
5、public void trimToSize()
該方法的作用是將ArrayList的容量變得和當前數組中元素的個數一樣。代碼如下:
public void trimToSize() {
int s = size;
if (s == array.length) {
return;
}
if (s == 0) {
array = EmptyArray.OBJECT;
} else {
Object[] newArray = new Object[s];
System.arraycopy(array, 0, newArray, 0, s);
array = newArray;
}
modCount++;
}
由上述代碼可知,有三種情況:數組中元素剛剛和ArrayList容量相等、數組中元素個數爲零和數組中元素個數比容量小,然後對數組進行“縮容”,再拷貝原元素。
6、public int hashCode()
該方法的作用是求ArrayList中哈希值的綜合,示例大嗎如下:
public int hashCode() {
Object[] a = array;
int hashCode = 1;
for (int i = 0, s = size; i < s; i++) {
Object e = a[i];
hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode());
}
return hashCode;
}
由上述代碼可知,ArrayList的哈希值等於數組中每個非空元素的哈希值相加的總和*31。
7、public boolean equals(Object o)
該方法的作用是比較兩個ArrayList是否相等,代碼如下:
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof List)) {
return false;
}
List<?> that = (List<?>) o;
int s = size;
if (that.size() != s) {
return false;
}
Object[] a = array;
if (that instanceof RandomAccess) {
for (int i = 0; i < s; i++) {
Object eThis = a[i];
Object ethat = that.get(i);
if (eThis == null ? ethat != null : !eThis.equals(ethat)) {
return false;
}
}
} else { // Argument list is not random access; use its iterator
Iterator<?> it = that.iterator();
for (int i = 0; i < s; i++) {
Object eThis = a[i];
Object eThat = it.next();
if (eThis == null ? eThat != null : !eThis.equals(eThat)) {
return false;
}
}
}
return true;
}
由上述代碼可以看出複雜對象比較的一般思路:
1>比較是否是同一對象。
2>比較是否是同一類型。
3>比較元素個數。
4>比較是否是RandomAccess類型。
5>逐個遍歷元素,進行比較。
4.6 常用遍歷方法
ArrayList常用的遍歷方法有3種:Iterator遍歷、普通for循環和foreach循環。
1>Iterator遍歷,示例代碼如下:
Iterator<String> it = testList.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
2>普通for循環,示例代碼如下:
for(int i = 0 ; i < testList.size() ; i++){
System.out.println(testList.get(i));
}
3>foreach循環,示例代碼如下:
for(String string:testList){
System.out.println(string);
}
五、Vector
Vector繼承AbstractList,實現List接口。
Vector特點:
1>Vector 繼承了AbstractList,實現了List接口。
2>Vector實現了RandmoAccess接口,即提供了隨機訪問功能。
3>Vector 實現了Cloneable接口,即實現克隆功能。
4>Vector 實現Serializable接口,表示支持序列化。
Vector是線程安全的。
5.1 構造方法
1>public Vector()
構造一個空向量,使其內部數據數組的大小爲 10,其標準容量增量爲零。
2>public Vector(int capacity)
使用指定的初始容量和等於零的容量增量構造一個空向量。
3>public Vector(int capacity, int capacityIncrement)
使用指定的初始容量和容量增量構造一個空的向量 。
4>public Vector(Collection<? extends E> collection)
構造一個包含指定 collection 中的元素的向量。
5.2 添加元素
1>public void add(int location, E object)
在向量的指定位置插入指定的元素。
2>public synchronized boolean add(E object)
將指定元素添加到此向量的末尾。
3>public synchronized boolean addAll(int location, Collection<? extends E> collection)
在指定位置將指定 Collection 中的所有元素插入到此向量中。
4>public synchronized boolean addAll(Collection<? extends E> collection)
將指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的順序添加這些元素。
5>public synchronized void addElement(E object)
將指定的組件添加到此向量的末尾。
5.3 刪除元素
1>public synchronized E remove(int location)
移除此向量中指定位置的元素。
2>public boolean remove(Object object)
移除此向量中指定元素的第一個匹配項,如果向量不包含該元素,則元素保持不變。更確切地講,移除其索引 i 滿足 (o == null ? get(i)==null : o.equals(get(i))) 的元素。
3>public synchronized boolean removeAll(Collection<?> collection)
從此向量中移除包含在指定 Collection 中的所有元素。
4>public synchronized void removeAllElements()
從此向量中移除全部組件,並將其大小設置爲零。
5>public synchronized boolean removeElement(Object object)
從此向量中移除變量的第一個(索引最小的)匹配項。
6>public synchronized void removeElementAt(int location)
刪除指定索引處的組件。
7>public void clear()
從此向量中移除所有元素。
5.4 刪除元素
1>public synchronized int capacity()
返回此向量的當前容量。
2>public synchronized Object clone()
返回向量的一個副本。
3>public boolean contains(Object object)
如果此向量包含指定的元素,則返回 true。更確切地講,當且僅當此向量至少包含一個滿足 (o == null ? e == null : o.equals(e)) 的元素 e 時,返回 true。
4>public synchronized boolean containsAll(Collection<?> collection)
如果此向量包含指定 Collection 中的所有元素,則返回 true。
5>public synchronized void copyInto(Object[ ] elements)
將此向量的組件複製到指定的數組中。
6>public synchronized E elementAt(int location)
返回指定索引處的元素。
7>public Enumeration elements()
返回此向量的組件的枚舉。
8>public synchronized void ensureCapacity(int minimumCapacity)
增加此向量的容量(如有必要),以確保其至少能夠保存最小容量參數指定的組件數。
9>public synchronized boolean equals(Object object)
比較指定對象與此向量的相等性。當且僅當指定的對象也是一個 List、兩個 List 大小相同,並且其中所有對應的元素對都相等 時才返回 true。
10>public synchronized E firstElement()
返回此向量的第一個組件 。
11>public E get(int location)
返回向量中指定位置的元素。
12>public synchronized int hashCode()
返回此向量的哈希碼值。
13>public int indexOf(Object object)
返回此向量中第一次出現的指定元素的索引,如果此向量不包含該元素,則返回 -1。更確切地講,返回滿足 (o == null ? get(i) == null : o.equals(get(i))) 的最低索引 i;如果沒有這樣的索引,則返回 -1。
14>public synchronized int indexOf(Object object, int location)
返回此向量中第一次出現的指定元素的索引,從 index 處正向搜索,如果未找到該元素,則返回 -1。更確切地講,返回滿足 (i >= index && (o==null ? get(i) == null : o.equals(get(i)))) 的最低索引 i;如果沒有這樣的索引,則返回 -1 |
15>public synchronized void insertElementAt(E object, int location)
將指定對象作爲此向量中的組件插入到指定的 index 處。
16>public synchronized boolean isEmpty()
測試此向量是否不包含組件。
17>public synchronized E lastElement()
返回此向量的最後一個組件。
18>public synchronized int lastIndexOf(Object object)
返回此向量中最後一次出現的指定元素的索引;如果此向量不包含該元素,則返回 -1。更確切地講,返回滿足 (o == null ? get(i)==null : o.equals(get(i))) 的最高索引 i;如果沒有這樣的索引,則返回 -1。
19>public synchronized int lastIndexOf(Object object, int location)
返回此向量中最後一次出現的指定元素的索引,從 index 處逆向搜索,如果未找到該元素,則返回 -1。更確切地講,返回滿足 (i <= index && (o == null ? get(i)==null : o.equals(get(i)))) 的最高索引 i;如果沒有這樣的索引,則返回 -1。
20>public synchronized boolean retainAll(Collection<?> collection)
在此向量中僅保留包含在指定 Collection 中的元素,即求交集。
21>public synchronized E set(int location, E object)
用指定的元素替換此向量中指定位置處的元素。
22>public synchronized void setElementAt(E object, int location)
將此向量指定 index 處的組件設置爲指定的對象。
23>public synchronized void setSize(int length)
設置此向量的大小。如果新大小大於當前大小,則會在向量的末尾添加相應數量的 null 項。如果新大小小於當前大小,則丟棄索引 newSize 處及其之後的所有項。
24>public synchronized int size()
返回此向量中的組件數。
25>public synchronized List subList(int start, int end)
返回此 List 的部分視圖,元素範圍爲從 start(包括)到 end(不包括)。
26>public synchronized Object[ ] toArray()
將Vector中元素以數組形式返回。
27>public synchronized T[] toArray(T[] contents)
返回一個數組,包含此向量中以恰當順序存放的所有元素;返回數組的運行時類型爲指定數組的類型。
28>public synchronized String toString()
返回此向量的字符串表示形式。
29>public synchronized void trimToSize()
對此向量的容量進行微調,使其等於向量的當前大小。
5.5 特殊方法
除了線程安全以外,Vector的大部分功能實現與ArrayList並無太大區別,其中的一個較明顯區別是:ArrayList默認擴容1.5倍,而Vector是2倍。
1>public Vector(int capacity, int capacityIncrement)
public Vector(int capacity, int capacityIncrement) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity < 0: " + capacity);
}
elementData = newElementArray(capacity);
elementCount = 0;
this.capacityIncrement = capacityIncrement;
}
該方法是Vector的構造方法之一,其中capacity爲初始容量,capacityIncrement爲增長因子,如果該增長因子指定了,那麼擴容的時候會每次新的數組大小會在原數組的大小基礎上加上增長因子;如果不指定增長因子,那麼就給原數組大小*2。
5.6 常用遍歷方法
Vector常用的遍歷方法有4種:Iterator遍歷、Enumeration遍歷、普通for循環和foreach循環。
1>Iterator遍歷,示例代碼如下:
Iterator<String> it = vectorTest.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
2>Enumeration遍歷,示例代碼如下:
Enumeration<String> enume = ((Vector<String>) vectorTest).elements();
while(enume.hasMoreElements()){
System.out.println(enume.nextElement().toString());
}
3>普通for循環遍歷,示例代碼如下:
for (int i = 0; i < vectorTest.size(); i++) {
System.out.println(vectorTest.get(i));
}
4>foreach循環遍歷,示例代碼如下:
for (String string : vectorTest) {
System.out.println(string);
}
六、Stack
Stack繼承Vector。Stack 類表示後進先出(LIFO)的對象堆棧。它通過五個操作對類 Vector 進行了擴展 ,允許將向量視爲堆棧。它提供了通常的 push 和 pop 操作,以及取堆棧頂點的 peek 方法、測試堆棧是否爲空的 empty 方法、在堆棧中查找項並確定到堆棧頂距離的 search 方法。
Stack是線程安全的。
6.1 所有方法
1>public Stack()
創建一個空堆棧 。
2>public boolean empty()
測試堆棧是否爲空 。
3>public synchronized E peek()
查看堆棧頂部的對象,但不從堆棧中移除它。
4>public synchronized E pop()
移除堆棧頂部的對象,並作爲此函數的值返回該對象。
5>public E push(E object)
把項壓入堆棧頂部 。
6>public synchronized int search(Object o)
返回對象在堆棧中的位置,以 1 爲基數。
6.2 特殊方法
1>public synchronized E peek()
該方法的作用是:查看堆棧頂部的對象,但不從堆棧中移除它。
2>public synchronized E pop()
該方法的作用是:移除堆棧頂部的對象,並作爲此函數的值返回該對象。
3>public E push(E object)
元素入棧。
4>public synchronized int search(Object o)
檢索元素所在的位置,代碼如下:
public synchronized int search(Object o) {
final Object[] dumpArray = elementData;
final int size = elementCount;
if (o != null) {
for (int i = size - 1; i >= 0; i--) {
if (o.equals(dumpArray[i])) {
return size - i;
}
}
} else {
for (int i = size - 1; i >= 0; i--) {
if (dumpArray[i] == null) {
return size - i;
}
}
}
return -1;
}
該方法特殊的地方在於返回值,不是將i直接返回,而是返回的size - i。
6.3 常用遍歷方法
Stack常用的遍歷方法有2種:棧彈出遍歷和foreach循環遍歷。
1>棧彈出遍歷,示例代碼如下:
while (!testStack.isEmpty()) {
System.out.println(((Stack<String>) testStack).pop());
}
2>foreach循環遍歷,示例代碼如下:
for (String str : testStack) {
System.out.println(str);
}