首頁  >  文章  >  Java  >  關於vector類別的實例方法詳解

關於vector類別的實例方法詳解

Y2J
Y2J原創
2017-05-06 13:26:222044瀏覽

這篇文章主要介紹了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修飾。

【相關推薦】

1.

Java免費影片教學

#2.

Java初級入門影片教學

##3. FastJson開發教學

###

以上是關於vector類別的實例方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn