1
I’m having a hard time finding the error in a logic that I use for traversing an arraylist.
List<Caneta> canetas = new ArrayList<>();
canetas.add(c1);
canetas.add(c2);
System.out.print(Arrays.toString(canetas.toArray()));
for (Caneta caneta : canetas) {
int x = 0;
System.out.printf(canetas.get(x).getModelo());
x++;
}
The Sout returns me the same answer, as if the x had not self-improved, could someone explain the problem to me? Grateful.
Show the creation code of
c1
andc2
– Sorack
You must declare the
x
out of the loop for, the way it is reset to each iteration– Math
To go through the list with for each is not required counter, each loop is already incremented. In the translation for each is 'for each'.
– Marcos Paulo Rosa