Vector:动态数组的使用和说明
生活随笔
收集整理的這篇文章主要介紹了
Vector:动态数组的使用和说明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘自百度百科:
1.?Vector 類在 java 中可以實現自動增長的對象數組;
創建了一個向量類的對象后,可以往其中隨意地插入不同的類的對象,既不需顧及類型也不需預先選定向量的容量,并可方便地進行查找。對于預先不知或不愿預先定義數組大小,并需頻繁進行查找、插入和刪除工作的情況,可以考慮使用向量類。向量類提供了三種構造方法: public Vector() public Vector(int initialcapacity,int capacityIncrement) public Vector(int initialcapacity) 使用第一種方法,系統會自動對向量對象進行管理。若使用后兩種方法,則系統將根據參數initialcapacity設定向量對象的容量(即向量對象可存儲數據的大小),當真正存放的數據個數超過容量時,系統會擴充向量對象的存儲容量。 參數capacityIncrement給定了每次擴充的擴充值。當capacityIncrement為0時,則每次擴充一倍。利用這個功能可以優化存儲。 2. Java中,數組對象一旦創建后,其元素的個數 不能被修改。而Java.util包中的Vector類(向量)提供類似于數組的能力,且能夠動態地調整自身的大小。Vector類似于一個數組,但與數組相比在使用上有兩個優點: ① 使用的時候無須聲明上限,隨著元素的增加,Vector的長度會自動增加; ② Vector類提供額外的方法來增加、刪除元素,比數組操作高效。[1]
插入功能
(1)public final synchronized void addElement(Object obj) 將obj插入向量的尾部。obj可以是任何類的對象。對同一個向量對象,可在其中插入不同類的對象。但插入的應是對象而不是數值,所以插入數值時要注意將數值轉換成相應的對象。 例 要插入一個整數1時,不要直接調用v1.addElement(1),正確的方法為:| 1 2 3 | Vectorv1=new?Vector(); Integerinteger1=new?Integer(1); v1.addElement(integer1); |
刪除功能
(1)public final synchronized void removeElement(Object obj) 從向量中刪除obj。若有多個存在,則從向量頭開始試,刪除找到的第一個與obj相同的向量成員。 (2)public final synchronized void removeAllElement() 刪除向量中所有的對象。 (3)public final synchronized void removeElementlAt(int index) 刪除index所指的地方的對象。查詢搜索功能
(1)public final int indexOf(Object obj) 從向量頭開始搜索obj,返回所遇到的第一個obj對應的下標,若不存在此obj,返回-1。 (2)public final synchronized int indexOf(Object obj,int index) 從index所表示的下標處開始搜索obj。 (3)public final int lastIndexOf(Object obj) 從向量尾部開始逆向搜索obj。 (4)public final synchronized int lastIndexOf(Object obj,int index) 從index所表示的下標處由尾至頭逆向搜索obj。 (5)public final synchronized Object firstElement() 獲取向量對象中的首個obj。 (6)public final synchronized Object lastElement() 獲取向量對象中的最后一個obj。實例
了解了向量的最基本的方法后,我們來看一下例子VectorApp.java。 例 VectorApp.java| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | importjava.util.Vector; importjava.lang.*; //這一句不應該要,但原文如此 importjava.util.Enumeration; public?class?VectorApp { ????public?static?void?main(String[]args) ????{ ????????Vector<Integer>?v1=new?Vector<Integer>();//jdk1.5以后增加了對的支持! ????????Integer?integer1=new?Integer(1); ????????/** ????????*因為Vector<Integer>已經指定為Integer類型 ????????*當v1.addElement("one")時會報錯,類型不符 ????????*ThemethodaddElement(Integer)inthetypeVector<Integer>isnotapplicablefor ????????*thearguments(String) ????????*/ ????????//加入的為字符串對象 ????????v1.addElement("one"); ????????v1.addElement("two"); ????????//加入的為Integer的對象 ????????v1.addElement(integer1); ????????v1.addElement(newInteger(2)); ????????System.out.println("Thevectorv1is:\n\t"+v1); ????????//此處的輸出結果為:Thevectorv1is:[1,2] ????????/** ????????*ThemethodinsertElementAt(Integer,int)inthetypeVector<Integer>isnotapplicablefor ????????*thearguments(String,int) ????????*insertElementAt()是指:Insertsthespecifiedobjectasacomponentinthisvectorat ????????*thespecifiedindex. ????????*/ ????????//將v1轉換成字符串并打印 ????????v1.insertElementAt("three",2); ????????v1.insertElementAt(newFloat(3.9),3); ????????System.out.println("Thevectorv1(usedmethodinsertElementAt())is:\n\t"+v1); ????????//以上運行,請改Vector<Integer>為Vector<Float> ????????/** ????????*setElementAt(Eobj,intindex)的用法 ????????*Setsthecomponentatthespecifiedindexofthisvectortobethespecifiedobject. ????????*Thepreviouscomponentatthatpositionisdiscarded.?????????????? ????????*Theindexmustbeavaluegreaterthanorequalto0andlessthanthecurrentsizeofthevector. ????????*/ ????????//將指定位置的對象設置為新的對象 ????????v1.setElementAt(2,1); ????????System.out.println("Thevectorv1(usedmethodsetElementAt())is:\n\t"+v1); ????????//從向量對象v1中刪除對象integer1由于存在多個integer1所以從頭開始找,刪除找到的第一個integer1 ????????v1.removeElement(integer1); ????????//使用枚舉類(Enumeration)的方法來獲取向量對象的每個元素 ????????Enumerationenum=v1.elements(); ????????System.out.print("Thevectorv1(usedmethodremoveElement())is:"); ????????while(enum.hasMoreElements()) ????????System.out.print(enum.nextElement()+""); ????????System.out.println(); ????????System.out.println("Thepositionofobject1(top-to-bottom):" +v1.indexOf(integer1)); ????????System.out.println("Thepositionofobject1(tottom-to-top):" +v1.lastIndexOf(integer1)); ????????//按不同的方向查找對象integer1所處的位置 ????????v1.setSize(4); ????????System.out.println("Thenewvector(resizedthevector)is:"+v1); ????????//重新設置v1的大小,多余的元素被行棄 ????} } |
插入功能
(1)public final synchronized void addElement(Object obj) 將obj插入向量的尾部。obj可以是任何類的對象。對同一個向量對象,可在其中插入不同類的對象。但插入的應是對象而不是數值,所以插入數值時要注意將數值轉換成相應的對象。 例 要插入一個整數1時,不要直接調用v1.addElement(1),正確的方法為:| 1 2 3 | Vectorv1=new?Vector(); Integerinteger1=new?Integer(1); v1.addElement(integer1); |
刪除功能
(1)public final synchronized void removeElement(Object obj) 從向量中刪除obj。若有多個存在,則從向量頭開始試,刪除找到的第一個與obj相同的向量成員。 (2)public final synchronized void removeAllElement() 刪除向量中所有的對象。 (3)public final synchronized void removeElementlAt(int index) 刪除index所指的地方的對象。查詢搜索功能
(1)public final int indexOf(Object obj) 從向量頭開始搜索obj,返回所遇到的第一個obj對應的下標,若不存在此obj,返回-1。 (2)public final synchronized int indexOf(Object obj,int index) 從index所表示的下標處開始搜索obj。 (3)public final int lastIndexOf(Object obj) 從向量尾部開始逆向搜索obj。 (4)public final synchronized int lastIndexOf(Object obj,int index) 從index所表示的下標處由尾至頭逆向搜索obj。 (5)public final synchronized Object firstElement() 獲取向量對象中的首個obj。 (6)public final synchronized Object lastElement() 獲取向量對象中的最后一個obj。實例
了解了向量的最基本的方法后,我們來看一下例子VectorApp.java。 例 VectorApp.java| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | importjava.util.Vector; importjava.lang.*; //這一句不應該要,但原文如此 importjava.util.Enumeration; public?class?VectorApp { ????public?static?void?main(String[]args) ????{ ????????Vector<Integer>?v1=new?Vector<Integer>();//jdk1.5以后增加了對的支持! ????????Integer?integer1=new?Integer(1); ????????/** ????????*因為Vector<Integer>已經指定為Integer類型 ????????*當v1.addElement("one")時會報錯,類型不符 ????????*ThemethodaddElement(Integer)inthetypeVector<Integer>isnotapplicablefor ????????*thearguments(String) ????????*/ ????????//加入的為字符串對象 ????????v1.addElement("one"); ????????v1.addElement("two"); ????????//加入的為Integer的對象 ????????v1.addElement(integer1); ????????v1.addElement(newInteger(2)); ????????System.out.println("Thevectorv1is:\n\t"+v1); ????????//此處的輸出結果為:Thevectorv1is:[1,2] ????????/** ????????*ThemethodinsertElementAt(Integer,int)inthetypeVector<Integer>isnotapplicablefor ????????*thearguments(String,int) ????????*insertElementAt()是指:Insertsthespecifiedobjectasacomponentinthisvectorat ????????*thespecifiedindex. ????????*/ ????????//將v1轉換成字符串并打印 ????????v1.insertElementAt("three",2); ????????v1.insertElementAt(newFloat(3.9),3); ????????System.out.println("Thevectorv1(usedmethodinsertElementAt())is:\n\t"+v1); ????????//以上運行,請改Vector<Integer>為Vector<Float> ????????/** ????????*setElementAt(Eobj,intindex)的用法 ????????*Setsthecomponentatthespecifiedindexofthisvectortobethespecifiedobject. ????????*Thepreviouscomponentatthatpositionisdiscarded.?????????????? ????????*Theindexmustbeavaluegreaterthanorequalto0andlessthanthecurrentsizeofthevector. ????????*/ ????????//將指定位置的對象設置為新的對象 ????????v1.setElementAt(2,1); ????????System.out.println("Thevectorv1(usedmethodsetElementAt())is:\n\t"+v1); ????????//從向量對象v1中刪除對象integer1由于存在多個integer1所以從頭開始找,刪除找到的第一個integer1 ????????v1.removeElement(integer1); ????????//使用枚舉類(Enumeration)的方法來獲取向量對象的每個元素 ????????Enumerationenum=v1.elements(); ????????System.out.print("Thevectorv1(usedmethodremoveElement())is:"); ????????while(enum.hasMoreElements()) ????????System.out.print(enum.nextElement()+""); ????????System.out.println(); ????????System.out.println("Thepositionofobject1(top-to-bottom):" +v1.indexOf(integer1)); ????????System.out.println("Thepositionofobject1(tottom-to-top):" +v1.lastIndexOf(integer1)); ????????//按不同的方向查找對象integer1所處的位置 ????????v1.setSize(4); ????????System.out.println("Thenewvector(resizedthevector)is:"+v1); ????????//重新設置v1的大小,多余的元素被行棄 ????} } |
總結
以上是生活随笔為你收集整理的Vector:动态数组的使用和说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [WARNING] The POM fo
- 下一篇: ANSI,ASCII,Unicode的区