Java中的vector類(lèi)使用示例小結(jié)
基本操作示例
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); //加入為字符串對(duì)象 v1.addElement("one"); //加入的為integer的對(duì)象 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); //向指定位置插入新對(duì)象 v1.insertElement("three",2); v1.insertElement(new Float(3.9),3); System.out.println("The Vector v1(used method insertElementAt()is:\n\t)"+v1); //將指定位置的對(duì)象設(shè)置為新的對(duì)象 //指定位置后的對(duì)象依次往后順延 v1.setElementAt("four",2); System.out.println("The vector v1 cused method setElmentAt()is:\n\t"+v1); v1.removeElement(integer1); //從向量對(duì)象v1中刪除對(duì)象integer1 //由于存在多個(gè)integer1,所以從頭開(kāi)始。 //找刪除找到的第一個(gè)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(); //使用枚舉類(lèi)(Enumeration)的方法取得向量對(duì)象的每個(gè)元素。 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)); //按不同的方向查找對(duì)象integer1所處的位置 v1.setSize(4); System.out.println("The new Vector(resized the vector)is:"+v1); //重新設(shè)置v1的大小,多余的元素被拋棄 } }
運(yùn)行結(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倍擴(kuò)容
還記得ArrayList每次擴(kuò)容為元數(shù)組的0.5倍不?Vector在進(jìn)行擴(kuò)容操作時(shí)與ArrayList略微不同
protected int capacityIncrement;//用于指定每次擴(kuò)容的容量 private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);//如不指定capacityIncrement,默認(rèn)擴(kuò)容的容量為原數(shù)組的容量 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
細(xì)心的小伙伴可以發(fā)現(xiàn)Vector中多了一個(gè)capacityIncrement變量,該變量是用于指定每次擴(kuò)容的增量,如果不指定該變量,在grow中可以發(fā)現(xiàn)Vector默認(rèn)就擴(kuò)容為原數(shù)組的1倍
線(xiàn)程安全
Vertor是線(xiàn)程安全的!
Vertor源碼中另一個(gè)比較顯眼的地方就是絕大部分方法都有synchronized關(guān)鍵字,大家都知道這個(gè)關(guān)鍵字是用于線(xiàn)程同步的,所以Vector類(lèi)是線(xiàn)程安全的!
但是即使它所有的方法都被修飾成同步,也不意味著調(diào)用它的時(shí)候永遠(yuǎn)都不需要同步手段了:
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); } }
大家運(yùn)行此段代碼時(shí) 跑了一小段時(shí)間之后會(huì)發(fā)現(xiàn)有ArrayIndexOutOfBoundsException異常,這里Vector的get,remove,size方法盡管有synchronized修飾,但是在多線(xiàn)程環(huán)境中,如果不在方法端額外做同步措施的話(huà),這段代碼仍然是不安全的,如果一個(gè)線(xiàn)程刪除了序號(hào)i的元素之后,另一個(gè)線(xiàn)程去訪(fǎng)問(wèn)這個(gè)i的話(huà)就直接回拋異常,所以保證這段代碼安全還需要再run里面再添加synchronized修飾。
相關(guān)文章
Java?EE實(shí)現(xiàn)用戶(hù)后臺(tái)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?EE實(shí)現(xiàn)用戶(hù)后臺(tái)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Java進(jìn)階教程之運(yùn)行時(shí)類(lèi)型識(shí)別RTTI機(jī)制
這篇文章主要介紹了Java進(jìn)階教程之運(yùn)行時(shí)類(lèi)型識(shí)別RTTI機(jī)制,在Java運(yùn)行時(shí),RTTI維護(hù)類(lèi)的相關(guān)信息,比如多態(tài)(polymorphism)就是基于RTTI實(shí)現(xiàn)的,需要的朋友可以參考下2014-09-09springboot2中HikariCP連接池的相關(guān)配置問(wèn)題
這篇文章主要介紹了springboot2中HikariCP連接池的相關(guān)配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12基于Spring上下文工具類(lèi)?ApplicationContextUtil
這篇文章主要介紹了基于Spring上下文工具類(lèi)?ApplicationContextUtil,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11使用ehcache三步搞定springboot緩存的方法示例
本次內(nèi)容主要介紹基于Ehcache 3.0來(lái)快速實(shí)現(xiàn)Spring Boot應(yīng)用程序的數(shù)據(jù)緩存功能。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04