Turning 1D array to 2D array in TicTacToe To check a win in my TicTacToe game, I have created a 2D array that contains all the combination that a game can be won in, like so: 

 private int[][] winningCombinations = new int[][] {
            {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
            {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
            {0, 4, 8}, {2, 4, 6}             //diagonal wins
        };
JButton buttons[] = new JButton[9]; // This is what I had when working with 1D array
JButton buttons[][] = new JButton[3][3]; // This is what I have now


then I have used for loop to loop through this array like this:

/*Determine who won*/
        for(int i=0; i&lt;=7; i++){
            if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &amp;&amp; 
                buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &amp;&amp; 
                !buttons[winCombinations[i][0]].getText().equals("")) {
                win = true;
            }
        }

        for(int i = 0; i &lt; 3; i++){
            if (buttons[i*3].getText().equals("X") &amp;&amp; buttons[i*3+1].getText().equals("X") &amp;&amp; buttons[i*3+2].getText().equals(""))
                buttons[i*3+2].setText("O");
                win = true;
            }


Everything works, but now I am planning to convert my code from 1D to 2D array, but when I convert this to 2D, it does not work. I might be wrong, but this is how I attempted to convert to 2D:

for(int i=0; i&lt;=7; i++){
    if( buttons[winCombinations[i][0]][winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]][winCombinations[i][1]].getText()) &amp;&amp; 
        buttons[winCombinations[i][1]][winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]][winCombinations[i][2]].getText()) &amp;&amp; 
        !buttons[winCombinations[i][0]][winCombinations[i][0]].getText().equals("")) {
        win = true;
    }
}
Matrices ... 1D or 2D format

In computing it is common to have matrix-like problems. Graphics, mathematics, and Tic-Tac-Toe.

It is often easier to have the matrix data stored as a 1D array, or 'flattened'.

Whether you have it as a 1D or a 2D array does not really matter for the functional aspect, but it can often have a really big impact on the readability.

A matrix of M x N dimension would be stored, for example, as :

int[][] matrix = new int[M][N];


To 'visit' all members of that matrix you need to:

for (int r = 0; r &lt; M; r++) {
    for (int c = 0; c &lt; N; c++) {
        // do someting with matrix[r][c];
    }
}


As a flattened matrix, it would be:

int[] matrix = new int[M * N];


To visit the members, it is:

for (int i = 0; i &lt; matrix.length; i++) {
    // do something with matrix[i];
}


When you have complicated indexing, the flattene system can be more efficient.

Additionally, the flattened matrix has interesting memory properties that often leads to better performance on High-Performing Computational clusters.

Converting from one format to the other is relatively simple.

final int width = 3;
matrix1D[row * width + col] = matrix2D[row][col];
matrix2D[i / width][i % width] = matrix1D[i];


Review


In your case, your matrix is small, and you should chose whichever format makes the code most readable.

If you choose a 2D implementation, you should make sure your winning combinations array is expressed as a 2D format....

int[][][] winningcombinations = {
  { {0, 0}, {0, 1}, {0, 2} }, // Top
  { {1, 0}, {1, 1}, {1, 2} }, // Middle
  { {2, 0}, {2, 1}, {2, 2} }, // Bottom
  { {0, 0}, {1, 0}, {2, 0} }, // left
  .......
}

Magic Numbers

Java arrays have a length property. Use it. You have the code:


for(int i=0; i&lt;=7; i++){ .... }



This should be:

for (int i = 0; i &lt;= winCombinations.length; i++) { .... }

Code Style

In Java recommended code style puts spaces around operators and reserved words.


for(int i=0; i&lt;=7; i++){ .... }



should be (ignoring the 7)

for (int i = 0; i &lt;= 7; i++) { .... }