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); //轉(zhuǎn)為字符串并打印 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); //將指定位置的對象設(shè)置為新的對象 //指定位置后的對象依次往后順延 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); //重新設(shè)置v1的大小,多余的元素被拋棄 } }
運行結(jié)果:
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每次擴容為元數(shù)組的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,默認擴容的容量為原數(shù)組的容量 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
細心的小伙伴可以發(fā)現(xiàn)Vector中多了一個capacityIncrement變量,該變量是用于指定每次擴容的增量,如果不指定該變量,在grow中可以發(fā)現(xiàn)Vector默認就擴容為原數(shù)組的1倍
線程安全
Vertor是線程安全的!
Vertor源碼中另一個比較顯眼的地方就是絕大部分方法都有synchronized關(guān)鍵字,大家都知道這個關(guān)鍵字是用于線程同步的,所以Vector類是線程安全的!
但是即使它所有的方法都被修飾成同步,也不意味著調(diào)用它的時候永遠都不需要同步手段了:
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); } }
大家運行此段代碼時 跑了一小段時間之后會發(fā)現(xiàn)有ArrayIndexOutOfBoundsException
異常,這里Vector的get,remove,size
方法盡管有synchronized
修飾,但是在多線程環(huán)境中,如果不在方法端額外做同步措施的話,這段代碼仍然是不安全的,如果一個線程刪除了序號i的元素之后,另一個線程去訪問這個i的話就直接回拋異常,所以保證這段代碼安全還需要再run
里面再添加synchronized
修飾。
希望本篇vector類使用示例文章對您有所幫助
相關(guān)文章
springboot攔截器過濾token,并返回結(jié)果及異常處理操作
這篇文章主要介紹了springboot攔截器過濾token,并返回結(jié)果及異常處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09IntelliJ?IDEA?2020.2.3永久破解激活教程(親測有效)
intellij?idea?2022是一款市面上最好的JAVA?IDE編程工具,該工具支持git、svn、github等版本控制工具,整合了智能代碼助手、代碼自動提示等功能,本教程給大家分享IDEA?2022最新永久激活碼,感興趣的朋友參考下吧2020-10-10Java 線程池_動力節(jié)點Java學(xué)院整理
系統(tǒng)啟動一個新線程的成本是比較高的,因為它涉及到與操作系統(tǒng)的交互。在這種情況下,使用線程池可以很好的提供性能,尤其是當程序中需要創(chuàng)建大量生存期很短暫的線程時,更應(yīng)該考慮使用線程池2017-05-05