更新时间:2022-12-12 08:48:43
这是一个使用 Java 8 流的解决方案:
Here's a solution using Java 8 streams:
String line = "1,21,33";
List<Integer> ints = Arrays.stream(line.split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
或者,使用循环,只需使用 parseInt
:
String line = "1,21,33";
for (String s : line.split(",")) {
System.out.println(Integer.parseInt(s));
}
如果你真的想重新发明***,你也可以这样做:
If you really want to reinvent the wheel, you can do that, too:
String line = "1,21,33";
for (String s : line.split(",")) {
char[] chars = s.toCharArray();
int sum = 0;
for (int i = 0; i < chars.length; i++) {
sum += (chars[chars.length - i - 1] - '0') * Math.pow(10, i);
}
System.out.println(sum);
}