更新時間:2022-08-19 08:56:58 來源:動力節點 瀏覽2052次
Vector 實現了一個動態數組。它類似于 ArrayList,但有兩個不同之處
矢量是同步的。
Vector 包含許多不屬于集合框架的遺留方法。
如果您事先不知道數組的大小,或者您只需要一個可以在程序的整個生命周期內更改大小的數組,Vector 被證明是非常有用的。
以下是向量類提供的構造函數列表。
編號 | 構造函數和描述 |
---|---|
1 | 向量( )
此構造函數創建一個默認向量,其初始大小為 10。 |
2 |
向量(整數大小) 此構造函數接受一個等于所需大小的參數,并創建一個初始容量由大小指定的向量。 |
3 |
向量(整數大小,整數增量) 此構造函數創建一個向量,其初始容量由 size 指定,其增量由 incr 指定。增量指定每次向上調整向量大小時要分配的元素數。 |
4 |
矢量(集合 c) 此構造函數創建一個包含集合 c 的元素的向量。 |
除了從其父類繼承的方法外,Vector 還定義了以下方法。
編號 | 方法和說明 |
---|---|
1 |
無效添加(int索引,對象元素) 在此 Vector 中的指定位置插入指定元素。 |
2 |
布爾添加(對象 o) 將指定元素附加到此 Vector 的末尾。 |
3 |
boolean addAll(集合 c) 將指定 Collection 中的所有元素附加到此 Vector 的末尾,按照指定 Collection 的 Iterator 返回的順序。 |
4 |
boolean addAll(int index, Collection c) 將指定 Collection 中的所有元素插入到此 Vector 的指定位置。 |
5 |
無效添加元素(對象 obj) 將指定的組件添加到此向量的末尾,將其大小增加一。 |
6 |
整數容量() 返回此向量的當前容量。 |
7 |
無效清除() 從此向量中刪除所有元素。 |
8 |
對象克隆() 返回此向量的克隆。 |
9 |
布爾包含(對象元素) 測試指定的對象是否是此向量中的一個組件。 |
10 |
布爾包含所有(集合 c) 如果此向量包含指定 Collection 中的所有元素,則返回 true。 |
11 |
無效的copyInto(對象[] anArray) 將此向量的分量復制到指定的數組中。 |
12 |
對象 elementAt(int index) 返回指定索引處的組件。 |
13 |
枚舉元素() 返回此向量的組件的枚舉。 |
14 |
無效確保容量(int minCapacity) 如有必要,增加此向量的容量,以確保它至少可以容納最小容量參數指定的分量數。 |
15 |
布爾等于(對象 o) 將指定的 Object 與此向量進行比較是否相等。 |
16 |
對象第一個元素() 返回此向量的第一個組件(索引 0 處的項目)。 |
17 |
對象獲取(整數索引) 返回此向量中指定位置的元素。 |
18 |
整數哈希碼() 返回此向量的哈希碼值。 |
19 |
int indexOf(對象元素) 搜索給定參數的第一次出現,使用 equals 方法測試相等性。 |
20 |
int indexOf(Object elem, int index) 搜索給定參數的第一次出現,從索引開始搜索,并使用 equals 方法測試相等性。 |
21 |
void insertElementAt(對象 obj,int 索引) 將指定對象作為組件插入此向量中指定索引處。 |
22 |
布爾 isEmpty() 測試此向量是否沒有分量。 |
23 |
對象最后一個元素() 返回向量的最后一個分量。 |
24 |
int lastIndexOf(對象元素) 返回此向量中指定對象最后一次出現的索引。 |
25 |
int lastIndexOf(Object elem, int index) 從指定的索引開始向后搜索指定的對象,并返回一個索引。 |
26 |
對象刪除(整數索引) 移除此向量中指定位置的元素。 |
27 |
布爾刪除(對象 o) 刪除此向量中第一次出現的指定元素,如果向量不包含該元素,則它保持不變。 |
28 |
boolean removeAll(集合 c) 從此向量中刪除包含在指定 Collection 中的所有元素。 |
29 |
無效刪除所有元素() 從此向量中刪除所有組件并將其大小設置為零。 |
30 |
布爾刪除元素(對象 obj) 從此向量中刪除參數的第一個(最低索引)出現。 |
31 |
void removeElementAt(int index) removeElementAt(int 索引)。 |
32 |
受保護的無效刪除范圍(int fromIndex,int toIndex) 從此 List 中刪除索引在 fromIndex(包括)和 toIndex(不包括)之間的所有元素。 |
33 |
boolean retainAll(集合 c) 僅保留此向量中包含在指定 Collection 中的元素。 |
34 |
對象集(int index,對象元素) 將此向量中指定位置的元素替換為指定元素。 |
35 |
void setElementAt(對象 obj,int 索引) 將此向量的指定索引處的組件設置為指定對象。 |
36 |
無效 setSize(int newSize) 設置此向量的大小。 |
37 |
整數大小() 返回此向量中的分量數。 |
38 |
列表子列表(int fromIndex,int toIndex) 返回此 List 在 fromIndex(包括)和 toIndex(不包括)之間部分的視圖。 |
39 |
對象[] toArray() 以正確的順序返回包含此向量中所有元素的數組。 |
40 |
對象[] toArray(對象[] a) 以正確的順序返回包含此向量中所有元素的數組;返回數組的運行時類型是指定數組的運行時類型。 |
41 |
字符串 toString() 返回此向量的字符串表示形式,其中包含每個元素的字符串表示形式。 |
42 |
無效 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
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習