I’ve got a pretty simple loop:
int[] positions = {1, 0, 0}
//print content of positions
for (int i : positions)
{
if (i <= 0) i = -1;
}
//print content of positions
Now, what I would expect to get is:
array: 1, 0, 0
array: 1, -1, -1
but instead I get
array: 1, 0, 0
array: 1, 0, 0
Just… why?
Kind regards,
jellyfish
Because “
i” is a copy of an array element and not a reference to it 🙂You modify a local variable, not an array’s element
this code is equivalent to