0

元のボードの同じ列にある2つの隣接するブロックを交換して得られる新しいボードを作成しています。問題は、新しいボードが元のボードの内容を上書きすることです。

例えば:

 int[][] origBoard = { { 0, 4, 6 }, {  5, 3, 1 }, { 2, 8, 7 } };
 int[][] twinBoard = { { 0, 6, 4 }, { 5, 3, 1 }, { 2, 8, 7 } };

次のことを主張すると、origBoardはtwinBoardと同じになります。

 Board B = new Board(origBoard);
 Board b = B.twin();
 assertFalse("Calling twin() modifies the original Board.", B.equals(b));

私のコードは次のとおりです。

public class Board {

    private int[][] goalBoard;

    private final Node node;

    private class Node {
        private int[][] board;
        private int move;
        private Node next;
    }

    // construct a board from an N-by-N array of blocks
    // (where blocks[i][j] = block in row i, column j)
    public Board(int[][] blocks) {
        int N = blocks.length;

        goalBoard = new int[N][N];
        for (int i = 0; i < dimension(); i++) {
            for (int j = 0; j < dimension(); j++) {
                if (i == N - 1 && j == N - 1) {
                    goalBoard[i][j] = 0;
                } else {
                    goalBoard[i][j] = N * i + (j + 1);
                }
            }
        }

        // initial node
        node = new Node();
        node.board = blocks;
        node.move = 0;
        node.next = null;
    }

    // board dimension N
    public int dimension() {
        return goalBoard.length;
    }

    // a board obtained by exchanging two adjacent blocks in the same row
    public Board twin() {
        int[][] testBoardATwin = new int[dimension()][dimension()];
        testBoardATwin = node.board;
        int x = node.board[0][0];
        int y = node.board[0][1];

        // DEFAULT
        if (x != 0 && y != 0) {
            testBoardATwin[0][0] = y;
            testBoardATwin[0][1] = x;
        }
        // 2x2
        if (dimension() == 2 || y == 0) {
            if (x == 0 || y == 0) {
                x = node.board[1][0];
                y = node.board[1][1];
                testBoardATwin[1][1] = x;
                testBoardATwin[1][0] = y;
            }
        } else {
            if (x == 0) {
                testBoardATwin[0][1] = node.board[0][2];
                testBoardATwin[0][2] = y;
            }
        }

        Board board = new Board(testBoardATwin);
        return board;
    }

    // does this board equal y?
    public boolean equals(Object y) {
        Board testBoard = (Board) y;
        if (testBoard == null) {
            return false;
        }
        for (int i = 0; i < dimension(); i++) {
            for (int j = 0; j < dimension(); j++) {
                if (testBoard.node.board[i][j] != node.board[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

}

私は何が間違っているのですか?助けてください。ありがとうございました。

4

5 に答える 5

3
int[][] testBoardATwin = new int[dimension()][dimension()];
testBoardATwin = node.board;

これがあなたの問題です。作成したい場合はnew、すぐに古いものに変更してフォローアップしないでください。

しかし、コメントも正しいです。単純なコピーと変更がより理にかなっています。

于 2012-09-23T05:34:43.047 に答える
3

これが問題です:

int[][] testBoardATwin = new int[dimension()][dimension()];
testBoardATwin = node.board;

コードが新しい int[][] 配列を作成すると、すべてがうまく始まりますが、すぐにその新しい配列を破棄し、twin呼び出されたインスタンスに属するものだけを使用します。

代わりに、node.boardインデックスごとにインデックスを作成するか、 のようなものを使用する必要がありますArrays.copyOf

于 2012-09-23T05:34:50.937 に答える
3

node = new Node(); node.board = blocks;

そして、Board コンストラクターの同じトリッキーな場所です。入力配列をコピーするのではなく、クラス メンバー プロパティへの参照を割り当てます。

于 2012-09-23T05:54:38.243 に答える
0

多次元配列をディープ コピーするために、次のようにしました。

  private static int[][] copy2d(int[][] nums) {
            int[][] copy = new int[nums.length][];

            for (int i = 0; i < copy.length; i++) {
                    int[] member = new int[nums[i].length];
                    System.arraycopy(nums[i], 0, member, 0, nums[i].length);
                    copy[i] = member;
            }

            return copy;
        }

    int[][] testBoardATwin = copy2d(node.board);
于 2012-09-23T06:39:44.890 に答える
-1

オブジェクトを作成するには、次の手順に従う必要があります。

  • まず、クラスをファイナルにする必要があります
  • すべてのフィールドを final および private にします。
  • 「セッター」メソッドを提供しない
  • サブクラスがメソッドをオーバーライドすることを許可しないでください。
  • 状態を変更するメソッドがないことに注意してください

そして、あなたが参照できる最高のリファレンスの1つ http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

于 2012-09-23T05:40:30.967 に答える