2012-02-17 186 views
1

是否有某種方法可將Vector<String>轉換爲Java中的Vector<Integer>將字符串轉換爲整數

我得到一個串矢量:

final Vector<String> partitions = (Vector<String>) properties.get(index); 

值是一個指數值,它是一個整數,總是。我需要將此字符串轉換爲整數。

回答

4

您需要手動將其轉換

Vector<String> strings = .. 
Vector<Integer> ints = ... 

for(String s : strings) { 
    ints.add(Integer.parseInt(s)); 
} 
0

有沒有辦法做到這一點,除了循環整個數組通,做投自己,然後重新添加到載體。

3

的方法是遍歷刺痛載體,解析每個元素把結果爲int向量:

List<Integer> intList = ....; 
for (String s : strList) { 
    intList.add(Integer.parseInt(s)); 
} 

如果你想寫這是單行承擔LambdaJ看看。

編輯:和忘記存在的矢量和散列表。改用ArrayList和HashMap。