2010-12-09 170 views

回答

16

呼叫向量的使用現有的集合(您的數組,在這種情況下)的構造函數初始化自己:

String[] strings = { "Here", "Are", "Some", "Strings" }; 
Vector<String> vector = new Vector<String>(Arrays.asList(strings)); 
+0

+1。我認爲這比使用泛型的職位要好。 – 2010-12-09 16:37:33

4
Vector<String> strVector = new Vector<String>(Arrays.asList(strArray)); 

打破下來:

  • Arrays.asList(array)將數組轉換爲List(其實現TS Collection

  • Vector(Collection)構造需要Collection和基於實例關閉它一個新的Vector

  • 我們通過新的ListVector構造函數來得到的String在數組新Vector,然後保存在strVector引用此對象。

+0

所以我可以直接做String [] myArray = {「hello」,「world」},然後在Vector的構造函數中引入Arrays.asList(myArray)? – Julio 2010-12-09 16:37:45

3
new Vector(Arrays.asList(array)) 
相關問題