Java Vector 類

2019-01-16 16:59 更新

Java Vector 類

Vector類實(shí)現(xiàn)了一個(gè)動(dòng)態(tài)數(shù)組。和ArrayList和相似,但是兩者是不同的:

  • Vector是同步訪問的。
  • Vector包含了許多傳統(tǒng)的方法,這些方法不屬于集合框架。

Vector主要用在事先不知道數(shù)組的大小,或者只是需要一個(gè)可以改變大小的數(shù)組的情況。

Vector類支持4種構(gòu)造方法。

第一種構(gòu)造方法創(chuàng)建一個(gè)默認(rèn)的向量,默認(rèn)大小為10:

Vector()

第二種構(gòu)造方法創(chuàng)建指定大小的向量。

Vector(int size)

第三種構(gòu)造方法創(chuàng)建指定大小的向量,并且增量用incr指定. 增量表示向量每次增加的元素?cái)?shù)目。

Vector(int size,int incr)

第四中構(gòu)造方法創(chuàng)建一個(gè)包含集合c元素的向量:

Vector(Collection c)

除了從父類繼承的方法外Vector還定義了以下方法:

序號(hào) 方法描述
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() 
返回此向量的當(dāng)前容量。
7 void clear() 
從此向量中移除所有元素。
8 Object clone() 
返回向量的一個(gè)副本。
9 boolean contains(Object elem) 
如果此向量包含指定的元素,則返回 true。
10 boolean containsAll(Collection c) 
如果此向量包含指定 Collection 中的所有元素,則返回 true。
11 void copyInto(Object[] anArray) 
 將此向量的組件復(fù)制到指定的數(shù)組中。
12 Object elementAt(int index) 
返回指定索引處的組件。
13 Enumeration elements() 
返回此向量的組件的枚舉。
14 void ensureCapacity(int minCapacity) 
增加此向量的容量(如有必要),以確保其至少能夠保存最小容量參數(shù)指定的組件數(shù)。
15 boolean equals(Object o) 
比較指定對(duì)象與此向量的相等性。
16 Object firstElement() 
返回此向量的第一個(gè)組件(位于索引 0) 處的項(xiàng))。
17 Object get(int index) 
返回向量中指定位置的元素。
18 int hashCode() 
返回此向量的哈希碼值。
19 int indexOf(Object elem) 
 返回此向量中第一次出現(xiàn)的指定元素的索引,如果此向量不包含該元素,則返回 -1。
20 int indexOf(Object elem, int index) 
 返回此向量中第一次出現(xiàn)的指定元素的索引,從 index 處正向搜索,如果未找到該元素,則返回 -1。
21 void insertElementAt(Object obj, int index) 
將指定對(duì)象作為此向量中的組件插入到指定的 index 處。
22 boolean isEmpty() 
測(cè)試此向量是否不包含組件。
23 Object lastElement() 
返回此向量的最后一個(gè)組件。
24 int lastIndexOf(Object elem) 
 返回此向量中最后一次出現(xiàn)的指定元素的索引;如果此向量不包含該元素,則返回 -1。
25 int lastIndexOf(Object elem, int index) 
返回此向量中最后一次出現(xiàn)的指定元素的索引,從 index 處逆向搜索,如果未找到該元素,則返回 -1。
26 Object remove(int index) 
 移除此向量中指定位置的元素。
27 boolean remove(Object o) 
移除此向量中指定元素的第一個(gè)匹配項(xiàng),如果向量不包含該元素,則元素保持不變。
28 boolean removeAll(Collection c) 
從此向量中移除包含在指定 Collection 中的所有元素。
29 void removeAllElements() 
從此向量中移除全部組件,并將其大小設(shè)置為零。
30 boolean removeElement(Object obj) 
從此向量中移除變量的第一個(gè)(索引最小的)匹配項(xiàng)。
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 處的組件設(shè)置為指定的對(duì)象。
36 void setSize(int newSize) 
 設(shè)置此向量的大小。
37 int size() 
 返回此向量中的組件數(shù)。
38 List subList(int fromIndex, int toIndex) 
返回此 List 的部分視圖,元素范圍為從 fromIndex(包括)到 toIndex(不包括)。
39 Object[] toArray()
 返回一個(gè)數(shù)組,包含此向量中以恰當(dāng)順序存放的所有元素。
40 Object[] toArray(Object[] a) 
返回一個(gè)數(shù)組,包含此向量中以恰當(dāng)順序存放的所有元素;返回?cái)?shù)組的運(yùn)行時(shí)類型為指定數(shù)組的類型。
41 String toString() 
返回此向量的字符串表示形式,其中包含每個(gè)元素的 String 表示形式。
42 void trimToSize() 
  對(duì)此向量的容量進(jìn)行微調(diào),使其等于向量的當(dāng)前大小。

實(shí)例

下面的程序說明這個(gè)集合所支持的幾種方法:

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();
   }
}

以上實(shí)例編譯運(yùn)行結(jié)果如下:

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
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)