Not*_*ere 0 java arrays netbeans
我正在编写一个数独求解器,我的老师建议我使用 3d 数组,因为我从未使用过 3d 数组;我无法弄清楚如何创建一个循环来遍历行和一个遍历列。你会怎么做呢?
编辑:我想出了如何遍历每第三列/行,希望我最终应该能够完成其他六列,但我是否朝着正确的方向前进?
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;
Run Code Online (Sandbox Code Playgroud)
您可以使用 3 个for循环来遍历 3D 数组,例如:
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]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,对于数独游戏,您不需要 3D 数组。二维数组就足够了。
| 归档时间: |
|
| 查看次数: |
7592 次 |
| 最近记录: |