Gautham Muralidharan wrote:Bu I don't understand how I get it. Please explain.
You could add a few additional println statements to see what's going on in the first enhanced for loop (as the second one should be pretty straightforward).
As the integer array is not initialized array[x] will always be 0 when x=1 and x=2, which means array[array[x]]=array[0] when x=1 and x=2. So in each iteration, array[0] is assigned with c++, which makes it 2 and the remaining two elements remain zero.