Bom dia a todos,
Estou com outra dúvida, segue abaixo o código:
package inquisition;
public class Test52{
public static void main(String[] args){
int []arr = {1,2,3,4};
for ( int i : arr )
{
System.out.println("i :"+i);
System.out.println("antes :"+arr[i]);
arr[i] = 0;
System.out.println("depois :"+arr[i]);
System.out.println();
}
for ( int i : arr )
{
System.out.println(i);
}
}
}
/*
i :1
antes :2
depois :0
i :0
antes :1
depois :0
i :3
antes :4
depois :0
i :0
antes :0
depois :0
0
0
3
0
The correct answer is D.
This so happens because arr[3] was made 0 during an iteration of the first "for in loop".
So during the final iteration of the first ??for in loop?? , we have 0 instead of 4 in the last index.
It can be seen from the second loop why an ArrayIndexOutOfBoundsException was not thrown at runtime.
The maximum number in the array is 3 which does not exceed the bound of the loop
*/
O que eu não entendi é porque o i é zerado?
arr[i] = 0;
Não estou zerando somente o índice do array?