1
友人が私にスドクパズルを解決しようとするクラスを教えてくれました。メソッド内の配列の変更は元の配列に反映されません。コードは次のとおりです。パラメータ渡しの参照をjavaで渡す
public static void solve(int array[][], int row, int col)
{
if(row > 8)
{
printBoard(array); // this gives the correct result
return;
}
if(array[row][col] != 0)
nextEmptyCell(array, row, col);
else
{
for(int num = 1; num < 10; num++)
{
if(validityRowColBox(array, row, col, num))
{
array[row][col] = num;
nextEmptyCell(array, row, col);
}
}
array[row][col] = 0;
}
}
public static void nextEmptyCell(int array[][], int row, int col)
{
if(col < 8)
solve(array, row, col + 1);
else
solve(array, row + 1, 0);
}
//This boolean methods will checks the validity of the number for the given row, columns and its designated box.
public static boolean validityRowColBox(int array[][], int row, int col, int num)
{
for(int c = 0; c <9; c++)
{
if(array[row][c] == num)
return false;// It return false if the number is already exist in the given row.
}
for(int r = 0; r <9; r++)
{
if(array[r][col] == num)
return false;// It return false if the number is already exist in the given columns.
}
row = (row/3) * 3 ;
col = (col/3) * 3 ;
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; c++)
{
if(array[row+r][col+c] == num)
return false;// It return false if the number is already exist in the designated box.
}
}
return true;// Otherwise true.
}
// sample input
public static int[][] easy()
{
return new int[][]
{{0,0,0,2,6,0,7,0,1},
{6,8,0,0,7,0,0,9,0},
{1,9,0,0,0,4,5,0,0},
{8,2,0,1,0,0,0,4,0},
{0,0,4,6,0,2,9,0,0},
{0,5,0,0,0,3,0,2,8},
{0,0,9,3,0,0,0,7,4},
{0,4,0,0,5,0,0,3,6},
{7,0,3,0,1,8,0,0,0}};
}
public static void main(String args[])
{
int inputArray[][] = easy();
solve(inputArray,0,0);
printBoard(inputArray); // items still the same!
}
}
私はprintBoard(配列)を呼び出します。関数の中で、配列の要素が変化するように見えます。しかし、私がprintBoard(配列)を呼び出すと、元の配列のメインメソッドのメソッドは、変更が失われ、値が再び元の元に戻ります。私はこれによって非常に混乱しています。メソッドのどれも新しいオブジェクトを作成しないので、inputArrayオブジェクトを指し示す必要があります。何が起こっていますか?
編集:ここでは(コメントを参照してください)
public static void printBoard(int array[][])
{
//This is the board method.
for (int row = 0; row < array.length; row++)
{
if (row % 3 == 0)//If the row is divisible by 3 then print the plus(+) and minus(-) sign.
System.out.println("+-------+-------+-------+");
for (int col = 0; col < array[row].length; col++) {
if (col % 3 == 0)// If the column is divisible by 3 then print the or(|) symbol.
System.out.print("| ");
System.out.print(array[row][col]+ " ");
}
System.out.println("|");
}
System.out.println("+-------+-------+-------+");
}
は、我々は 'printBoard'方法を見てもらえますか? – Berger
@Bergerを参照してください編集 – morbidCode