Your for each loop is Actually printing the third element of each winningPosition array inside 2D array winningPositions:
{{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}}
this statement winningPosition[2]
will get each third element of the inner array in the 2D array winningPositions
.
also you can break down for each loop to understand it well :
in the first iteration of for each loop it will get the first element of winningPositions:
{0,1,2}
then this statement winningPosition[2]
will get the third element in this sub array which is 2
in the second iteration of for each loop it will get the second element of winningPositions:
{3,4,5}
after that winningPosition[2]
will get the third element in this sub array which is 5
and so on.
it's much like getting winningPositions[1][2]
in the traditional for loop.
Update:
why is it not printing the third set of arrays {6,7,8} ?
I understand from your question that you want to print the third sub array of the 2D array winningPositions.
to print it you don't need any for each loop
you can simply use method Arrays.toString
to print it:
System.out.println(Arrays.toString(winningPositions[2]));
output:
[6, 7, 8]