String array = "19 35 91 12 36 48 59";
// separate them by space
String[] splited = array.split(" ");
// here we will save the numbers
int[] numbers = new int[splited.length];
for(int i = 0; i < splited.length; i++) {
numbers[i] = Integer.parseInt(splited[i]);
}
System.out.println(Arrays.toString(numbers));
3 回答
我拆分字符串,流式传输数组,分别解析每个元素并将它们收集到一个数组:
您可能也可以做这样的事情,即使它可能不像上面的解决方案那样漂亮:
如果字符串不是完全由数字组成,那么您将有一个部分为空的数组 . 根据您使用数组的内容,您可能希望使用其他内容 .
如果它们被空格分隔,你可以像这样逐个转换它们