1 import java.util.Vector; 2 import java.util.Iterator; 3 import java.util.Arrays; 4 import java.util.ArrayList; 5 public class VectorDemo{ 6 public static void main(String args[]){ 7 String names[]={"hfdsfng", "zhfcinyafdn"}; 8 String namesp[]=new String[]{"hv heng", "zhavxcyan"}; 9 Vector<String> v1=new Vector<String>( 10 Arrays.asList("hujghfgeng", "zhagfyan", "huaxvvidong") 11 ); 12 Vector<String> v2=new Vector<String>(Arrays.asList(names));//Arrays.asList()返回List列表(继承了Collection) 13 Vector<String> v3=new Vector<String>(new ArrayList<String>(){//匿名类的初始化,ArrayLists类实现了Collection接口 14 { 15 add("hujunzheng"); 16 add("zhangjinyan"); 17 } 18 }); 19 //三种实现遍历Vector的方法 20 System.out.println("V1向量:"); 21 for(Object tmp : v1.toArray()){ 22 System.out.println((String)tmp); 23 } 24 25 System.out.println("V2向量:"); 26 for(Iterator<String>it = v2.iterator(); it.hasNext(); ){ 27 System.out.println(it.next()); 28 } 29 30 System.out.println("V3向量:"); 31 for(int i=0; i<v3.size(); ++i){ 32 System.out.println(v3.elementAt(i)); 33 } 34 } 35 }
初始化的一些问题(Vector使用)
时间: 2024-10-08 02:58:49