You are missing something here. You have a List
of String
s, and you should iterate over the characters of each of these String
s separately in order to convert it to an int
. This means you need two nested loops.
Though there are much easier ways of doing it (with Integer.parseInt()
), here’s an implementation based on the logic of your original code:
List<String> numbers = Arrays.asList("45","9","77");
List<Integer> output = new ArrayList<>();
for (String number : numbers) {
int res = 0;
for (int i = 0; i < number.length(); i++) {
res = res * 10 + (number.charAt(i) - '0');
}
output.add(res);
}
System.out.println (output);
Output:
[45, 9, 77]
3
solved How to convert an ArrayList of Strings to integers without any library methods?