-2
int[][] 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}};

    for (int[] winningPosition : winningPositions)
    System.out.println(winningPosition[2]);

this prints 2 5 8 6 7 8 8 6

I dont understand why, this is so random, what is the foreach loop trying to do ? why is it not printing the third set of arrays {6,7,8} ? how does this work ?

Rechem MEG
  • 311
  • 1
  • 10
  • 1
    What exactly are you trying to print? – Mureinik Jul 22 '17 at 10:24
  • You don't have b[2] here, since b's size is 2. You can access b[0] or b[1]. b[2] will give you an exception. – TDG Jul 22 '17 at 10:26
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Joe C Jul 22 '17 at 10:37
  • just remember there are no real 2D arrays in Java - it is an array that contains array in each position. So for-each is returning each arras that is stored inside the outer array. `winningPositions[2]` would be {6, 7, 8} but the code is getting the value at position [2] of each inner array. – user85421 Jul 22 '17 at 11:04

4 Answers4

2

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 5and 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]
Oghli
  • 2,200
  • 1
  • 15
  • 37
0
int[][] 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}};

                for (int[] winningPosition : winningPositions){
                    for(int i = 0; i < winningPosition.length; i ++){
                        System.out.println(winningPosition[i]);
                   }
               }

Image having a drawer that have 1 note each in it. The note has x on top, in the middle y, and on the bottom z. With your loop you open every drawer and look at the third line of every note. Printing only z.

For each time you open the drawer you need to look at all the lines. So that you can for every line of the note print the contents. winningPosition.length is how many lines on that note there is.

Look up nested loops.

Lealo
  • 331
  • 1
  • 3
  • 11
0

Your for loop is going though each array and prints after it's done the integer it has in it's memory out. Since you typed:

for (int[] winningPosition : winningPositions)

You are asking to get int[] out of winningPositions. And so after it puts the 3 integers in such an array, it has the last integer in it's memory of the array he just configured. that's why it's only printing out the last integer of the array. if you wanna print out all of the original winningPositions, do:

for (int winningPosition : winningPosition)

if you wanna print out a specific number of each array in the original winningPosition, do:

for (int[] winningPosition : winningPosition)
    System.out.println(winningPosition[x];

X is the index spot of the integer you wanna print out. (the first index number of an array is always 0.) You can also use the last method I said to continue a selection you originally wanted to do. (By for example using another for loop to go through the int[] winningPosition you created in the previous for loop.)

Hope this helps you!

0

Iam providing something similar i am building at the moment.... this is printing all numbers and seperate arrays with new line and numbers with [], play a bit with the code and you will be able to print what you like...

public class ArrayFinal {

public static void main(String[] args) {

    System.out.println("Array\t Nummbers");
    int Array[][] = { { 2, 43, 23, 43, 23, 43 }, { 3, 5, 6, 7, 6, 5 }, { 34, 42, 44, 24, 6, 7 },
            { 2, 34, 54, 332, 23, 43 }, { 2, 43, 45, 54, 34, 34 }, { 2, 34, 54, 54, 34, 23 } };

    display(Array);

}


// methodos gia na dimiourgisoume to rows kai ta columns
public static void display(int x[][]) {


    for (int row = 0; row < x.length; row++) {

        StringBuilder str_buf = new StringBuilder();
        str_buf.append((row + 1) + ".");
        for (int column = 0; column < x[row].length; column++) {

            str_buf.append("\t" + "[" + x[row][column] + "]");// tiponi ta noumera pou vriskonte mesa stin klirosi


        }
        System.out.println(str_buf.toString());// tiponi to noumero tis klirosis

    }
}

}

results:

1.  [2] [43] [23] [43] [23] [43]
2.  [3] [5] [6] [7] [6] [5]
3.  [34] [42] [44] [24] [6] [7]
4.  [2] [34] [54] [332] [23] [43]
5.  [2] [43] [45] [54]  [34] [34]
6.  [2] [34] [54] [54]  [34] [23]  

iam new in java and since this post is active it would be great if someone can show me the way to displayOccurances from those arrays, i have tried almost everything....

Annu
  • 3
  • 7