[英]Convert vector from string to integer
是否有一些方法可以將Java中的Vector<String>
轉換為Vector<Integer>
?
我得到一個字符串向量:
final Vector<String> partitions = (Vector<String>) properties.get(index);
該值始終是整數的索引值。 我需要將此字符串轉換為整數。
您需要手動轉換
Vector<String> strings = ..
Vector<Integer> ints = ...
for(String s : strings) {
ints.add(Integer.parseInt(s));
}
方法是遍歷sting向量,解析每個元素以將結果放入int向量:
List<Integer> intList = ....;
for (String s : strList) {
intList.add(Integer.parseInt(s));
}
如果要單行編寫,請查看LambdaJ 。
編輯:並且請忘記Vector和Hashtable的存在。 請改用ArrayList和HashMap。
除了遍歷整個數組並自己進行強制轉換然后再次添加到向量外,沒有其他方法可以做到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.