java.util.vector中的vector的詳細用法


ArrayList會比Vector快,他是非同步的,如果設計涉及到多線程,還是用Vector比較好一些
import java.util.*;

 

/**
* 演示Vector的使用。包括Vector的創建、向Vector中添加元素、從Vector中刪除元素、
* 統計Vector中元素的個數和遍歷Vector中的元素。
*/

public class VectorDemo{
public static void main(String[] args){

//Vector的創建
//使用Vector的構造方法進行創建
Vector v = new Vector(4);

//向Vector中添加元素
//使用add方法直接添加元素
v.add("Test0");
v.add("Test1");
v.add("Test0");
v.add("Test2");
v.add("Test2");

//從Vector中刪除元素
v.remove("Test0"); //刪除指定內容的元素
v.remove(0); //按照索引號刪除元素

//獲得Vector中已有元素的個數
int size = v.size();
System.out.println("size:" + size);

//遍歷Vector中的元素
for(int i = 0;i < v.size();i++){
System.out.println(v.get(i));
}
}
}
-------------
Vector 類提供了實現可增長數組的功能,隨着更多元素加入其中,數組變的更大。在刪除一些元素之后,數組變小。
Vector 有三個構造函數,
public Vector(int initialCapacity,int capacityIncrement)          public Vector(int initialCapacity)          public Vector()   Vector 運行時創建一個初始的存儲容量initialCapacity,存儲容量是以capacityIncrement 變量定義的增量增長。初始的存儲容量和capacityIncrement 可以在Vector 的構造函數中定義。第二個構造函數只創建初始存儲容量。第三個構造函數既不指定初始的存儲容量也不指定capacityIncrement。   Vector 類提供的訪問方法支持類似數組運算和與Vector 大小相關的運算。類似數組的運算允許向量中增加,刪除和插入元素。它們也允許測試矢量的內容和檢索指定的元素,與大小相關的運算允許判定字節大小和矢量中元素不數目。   現針對經常用到的對向量增,刪,插功能舉例描述:
addElement(Object obj)     把組件加到向量尾部,同時大小加1,向量容量比以前大1  
insertElementAt(Object obj, int index)     把組件加到所定索引處,此后的內容向后移動1 個單位  
setElementAt(Object obj, int index)   把組件加到所定索引處,此處的內容被代替。   removeElement(Object obj) 把向量中含有本組件內容移走。   removeAllElements() 把向量中所有組件移走,向量大小為0。   

例如:

import java.lang.System;

import java.util.Vector;

import java.util.Emumeration;

public class Avector{                 

public static void main(String args[]) {
  0.Vector v=new Vector();
  1. v.addElement("one");
  2. addElement("two");
  3. v.addElement("three");
  4. v.insertElementAt("zero",0);
  5. v.insertElementAt("oop",3);
  6. v.setElementAt("three",3);
  7. v.setElementAt("four",4);
  8. v.removeAllElements();
}
}
Vector中的變化情況:
1. one   2. one   3. one   4. zero   5.zero   6. zero  7. zero 8.       two   two  one   one   one   one            three   two   two   two   two  three   oop   three  three  three   three  four     另外,Vector 在參數傳遞中發揮着舉足輕重的作用。在Applet 中有一塊畫布(Canvas) 和一個(Panel), 而Panel 中放着用戶要輸入的信息,根據這些信息把參數傳遞到canvas 中,這時在Java 中用一個接口(Interface), 而在接口中需用一個Vector 去傳遞這些參數。另外,在一個類向另一個類參數傳遞就可以用這種方法。   例如:  
import java.util.Vector
interface codeselect{ Vector codeselect=new Vector(); } 顯示數學信息
Vector(0)存入學生編號
Vector(1)存入學科     在Panel 中當用戶在TextField 和Choice 中選擇自己所要求的內容,程序中通過事件響應把值傳到向量Vector 中。

 

同步是個很大的問題,尤其多線程,和進程中,因此,我們在多線程中同時對某個數組操作時,支持同步的vector無疑是個很好的選擇,一般在需要將多個元素存在一個集合里的時候用。
java.util 類 Vector<E>
boolean add(E o)
將指定元素追加到此向量的末尾。
void add(int index, E element)
在此向量的指定位置插入指定的元素。
boolean addAll(Collection<? extends E> c)
將指定 Collection 中的所有元素追加到此向量的末尾,按照指定集合的迭代器所返回的順序追加這些元素。
boolean addAll(int index, Collection<? extends E> c)
在指定位置將指定 Collection 中的所有元素插入到此向量中。
void addElement(E obj)
將指定的組件添加到此向量的末尾,將其大小增加 1。
int capacity()
返回此向量的當前容量。
void clear()
從此向量中移除所有元素。
Object clone()
返回向量的一個副本。
boolean contains(Object elem)
測試指定的對象是否為此向量中的組件。
boolean containsAll(Collection<?> c)
如果此向量包含指定 Collection 中的所有元素,則返回 true。
void copyInto(Object[] anArray)
將此向量的組件復制到指定的數組中。
E elementAt(int index)
返回指定索引處的組件。
Enumeration<E> elements()
返回此向量的組件的枚舉。
void ensureCapacity(int minCapacity)
增加此向量的容量(如有必要),以確保其至少能夠保存最小容量參數指定的組件數。
boolean equals(Object o)
比較指定對象與此向量的相等性。
E firstElement()
返回此向量的第一個組件(位於索引 0 處的項)。
E get(int index)
返回向量中指定位置的元素。
int hashCode()
返回此向量的哈希碼值。
int indexOf(Object elem)
搜索給定參數的第一個匹配項,使用 equals 方法測試相等性。
int indexOf(Object elem, int index)
搜索給定參數的第一個匹配項,從 index 處開始搜索,並使用 equals 方法測試其相等性。
void insertElementAt(E obj, int index)
將指定對象作為此向量中的組件插入到指定的 index 處。
boolean isEmpty()
測試此向量是否不包含組件。
E lastElement()
返回此向量的最后一個組件。
int lastIndexOf(Object elem)
返回指定的對象在此向量中最后一個匹配項的索引。
int lastIndexOf(Object elem, int index)
向后搜索指定的對象,從指定的索引處開始搜索,並返回一個索引。
E remove(int index)
移除此向量中指定位置的元素。
boolean remove(Object o)
移除此向量中指定元素的第一個匹配項,如果向量不包含該元素,則元素保持不變。
boolean removeAll(Collection<?> c)
從此向量中移除包含在指定 Collection 中的所有元素。
void removeAllElements()
從此向量中移除全部組件,並將其大小設置為零。
boolean removeElement(Object obj)
從此向量中移除變量的第一個(索引最小的)匹配項。
void removeElementAt(int index)
刪除指定索引處的組件。
protected void removeRange(int fromIndex, int toIndex)
從此 List 中移除其索引位於 fromIndex(包括)與 toIndex(不包括)之間的所有元素。
boolean retainAll(Collection<?> c)
在此向量中僅保留包含在指定 Collection 中的元素。
E set(int index, E element)
用指定的元素替換此向量中指定位置處的元素。
void setElementAt(E obj, int index)
將此向量指定 index 處的組件設置為指定的對象。
void setSize(int newSize)
設置此向量的大小。
int size()
返回此向量中的組件數。
List<E> subList(int fromIndex, int toIndex)
返回此 List 的部分視圖,元素范圍為從 fromIndex(包括)到 toIndex(不包括)。
Object[] toArray()
返回一個數組,包含此向量中以正確順序存放的所有元素。
<T> T[]
toArray(T[] a)
返回一個數組,包含此向量中以正確順序存放的所有元素;返回數組的運行時類型為指定數組的類型。
String toString()
返回此向量的字符串表示形式,其中包含每個元素的 String 表示形式。
void trimToSize()
對此向量的容量進行微調,使其等於向量的當前大小。

參考文檔:

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the vector is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by the elements method are not fail-fast.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM