简体   繁体   中英

Iterate through a 3d array?

I'm coding a Sudoku solver and my teacher recommended that I use a 3d array and since I've never used 3d arrays; I'm having trouble figuring out how to create a loop to iterate through the rows and one through the columns. How would you go about doing this?

Edit: I figured out how to iterate through every third column/row and hopefully I should be able to do the other six eventually, but am I heading in the right direction?

 int[][][] = board[9][3][3];

public boolean columnCheck(int[][][] board)
{
    boolean filled = false;
    for(int i = 0; i < board.length; i++)
    {
        for(int j = 0; j < board[0].length; j++)
        {
            System.out.println(board[i][j][0]);                
        }

    }
    return true;
}

public boolean rowCheck(int[][][] board)
{
     boolean filled = false;
    for(int i = 0; i < board.length; i++)
    {
        for(int j = 0; j < board[0].length; j++)
        {
            System.out.println(board[i][0][j]);
        }

    }
    return true;

You can use 3 for loops to iterate through a 3D array, eg:

public static void main(String[] args) throws FileNotFoundException {
    int[][][] array = new int[9][3][3];
    for(int i=0 ; i<array.length ; i++){
        for(int j=0 ; j<array[i].length ; j++){
            for(int k=0 ; k<array[i][j].length ; k++){
                System.out.println("[" + i + "][" + j + "][" + k + "]:" + array[i][j][k]);
            }
        }
    }
}

However, for sudoku game, you don't need a 3D array. 2D array would suffice.

public class Main {

    public static void main(String[] args) {
        int[][][] board = new int[3][3][9];
        // Assume that first parameter is row
        // The second is column

        // Iterating through first row (board[0])
        for (int i = 0; i < 3; i++) {
            // i is col number
            for (int j = 0; j < 9; j++) {
                //j is block number
                System.out.println(board[0][i][j]);
            }
        }

        // Iterating through second column
        for (int i = 0; i < 3; i++) {
            // i is row number
            for (int j = 0; j < 9; j++) {
                // j is block number
                System.out.println(board[i][1][j]);
            }
        }
    }
}

I suppose that your 3d array represents the sudoku as follows: The '9' stands for the 9 small 3x3 blocks. The first '3' for each row of the block and the second '3' for the columns of each block.

That would give the following:

array[0][x][y]  |  array[1][x][y]  |  array[2][x][y] 
----------------------------------------------------
array[3][x][y]  |  array[4][x][y]  |  array[5][x][y]
----------------------------------------------------
array[6][x][y]  |  array[7][x][y]  |  array[8][x][y]

To iterate over each row you can do the following:

// The first three rows
// You can probably figure out yourself how to do the last 6, 
// and how to combine those 3 seperate sections
for (int i=0; i<3; i++) {
    for (int j=0; j<3; j++) {
        for (int k=0; j<3; k++) {
            System.out.println(array[j][i][k]);
        }
    }
}

// The first three columns 
for (int i=0; i<3; i++) {
    for (int j=0; j<7; j+=3) {
        for (int k=0; k<3; k++) {
            System.out.println(array[j][k][i]);
        }
    }
}

I hope this will get you going, without solving it all for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM