简体   繁体   中英

Convert vector from string to integer

Is there some method to convert a Vector<String> to a Vector<Integer> in Java ?

I am getting a string vector:

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

The value is an index value which is an integer, always. I need to convert this string into integer.

You need to manually convert it

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

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

The method is to iterate over the sting vector, parse each element to put the result to the int vector:

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

If you want to write this as single line take a look on LambdaJ .

EDIT: and please forget about existing of Vector and Hashtable. Use ArrayList and HashMap instead.

除了遍历整个数组并自己进行强制转换然后再次添加到向量外,没有其他方法可以做到。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM