如何在java中将字符串数组转换为int数组
问题描述:
我在这样的java程序中有一个字符串数组
I have an string array in java program like this
String results[] = { "2", "1", "5", "1" };
我想将此转换为整数数组,如下所示:
I want to convert this to integer array as like this:
int results[] = { 2, 1, 5, 1 };
最后我想找到所有 int $的总和c $ c>该数组的元素。
And finally I want to find the summation of all the int
elements of that array.
答
如果你使用的是java 8试试这个:
If you are using java 8 Try this :
int[] array = Arrays.stream(resultsStr).mapToInt(Integer::parseInt).toArray();