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());
Alternatively, with a loop, just use 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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…