Java中的vector類應用示例小結。本站提示廣大學習愛好者:(Java中的vector類應用示例小結)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中的vector類應用示例小結正文
根本操作示例
VectorApp.java
import java.util.Vector;
import java.lang.*;
import java.util.Enumeration;
public class VectorApp
{
public static void main(String args[])
{
Vector v1 = new Vector();
Integer integer1= new Integer(1);
//參加為字符串對象
v1.addElement("one");
//參加的為integer的對象
v1.addElement(integer1);
v1.addElement(integer1);
v1.addElement("two");
v1.addElement(new Integer(2));
v1.addElement(integer1);
v1.addElement(integer1);
//轉為字符串並打印
System.out.println("The Vector v1 is:\n\t"+v1);
//向指定地位拔出新對象
v1.insertElement("three",2);
v1.insertElement(new Float(3.9),3);
System.out.println("The Vector v1(used method
insertElementAt()is:\n\t)"+v1);
//將指定地位的對象設置為新的對象
//指定地位後的對象順次往後順延
v1.setElementAt("four",2);
System.out.println("The vector v1 cused method setElmentAt()is:\n\t"+v1);
v1.removeElement(integer1);
//從向量對象v1中刪除對象integer1
//因為存在多個integer1,所以從頭開端。
//找刪除找到的第一個integer1.
Enumeration enum = v1.elements();
System.out.println("The vector v1 (used method removeElememt()is");
while(enum.hasMoreElements())
System.out.println(enum.nextElement()+"");
System.out.println();
//應用列舉類(Enumeration)的辦法獲得向量對象的每一個元素。
System.out.println("The position of Object1(top-to-botton):"+v1.indexOf(integer1));
System.out.println("The position of Object1(tottom-to-top):"+v1.lastIndexOf(integer1));
//按分歧的偏向查找對象integer1所處的地位
v1.setSize(4);
System.out.println("The new Vector(resized the vector)is:"+v1);
//從新設置v1的年夜小,過剩的元素被擯棄
}
}
運轉成果:
E:\java01>java VectorApp The vector v1 is:[one,1,1,two,2,1,1] The vector v1(used method insetElementAt()) is: [one,1,three,3.9,1,two,2,1,1] The vector v1(used method setElementAt()) is: [one,1,four,3.9,1,two,2,1,1] The vector v1(useed method removeElement()) is: one four 3.9 1 two 2 1 1 The position of object1(top-to-botton):3 The position of object1(botton-to-top):7 The new Vector(resized the vector) is: [one,four,3.9,1]
Vertor的1倍擴容
還記得ArrayList每次擴容為元數組的0.5倍不?Vector在停止擴容操作時與ArrayList稍微分歧
protected int capacityIncrement;//用於指定每次擴容的容量
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);//如不指定capacityIncrement,默許擴容的容量為原數組的容量
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
仔細的小同伴可以發明Vector中多了一個capacityIncrement變量,該變量是用於指定每次擴容的增量,假如不指定該變量,在grow中可以發明Vector默許就擴容為原數組的1倍
線程平安
Vertor是線程平安的!
Vertor源碼中另外一個比擬顯眼的處所就是絕年夜部門辦法都有synchronized症結字,年夜家都曉得這個症結字是用於線程同步的,所以Vector類是線程平安的!
然則即便它一切的辦法都被潤飾成同步,也不料味著挪用它的時刻永久都不須要同步手腕了:
private static Vector<Integer> vector=new Vector<Integer>();
public static void main(String[] args) {
while(true)
{
for(int i=0;i<10;i++)
{
vector.add(i);
}
Thread removeThread=new Thread(new Runnable(){
@Override
public void run()
{
for(int i=0;i<vector.size();i++)
{
vector.remove(i);
}
}
});
Thread printThread=new Thread(new Runnable(){
@Override
public void run()
{
for(int i=0;i<vector.size();i++)
{
System.out.println(vector.get(i));
}
}
});
removeThread.start();
printThread.start();
while(Thread.activeCount()>20);
}
}
年夜家運轉此段代碼時 跑了一小段時光以後會發明有ArrayIndexOutOfBoundsException異常,這裡Vector的get,remove,size辦法雖然有synchronized潤飾,然則在多線程情況中,假如不在辦法端額定做同步辦法的話,這段代碼依然是不平安的,假如一個線程刪除序號i的元素以後,另外一個線程去拜訪這個i的話就直接回拋異常,所以包管這段代碼平安還須要再run外面再添加synchronized潤飾。