2012-09-26 125 views
0

我正在寫一個程序,其中我有一個板(3 x 3矩陣),我必須交換特定的行和列中的值與其相鄰的行和列邏輯,如果我在[0,0] 0值,那麼我想有兩個板。一個在[0,1]和[0,1] s處的值爲[0,0]處具有0值並且另一個板處於[1,0]和[1,0] s處的值爲[0, 0]。但是在執行下面的代碼之後,我有兩個具有相同值的板,我無法理解這些錯誤值的任何解釋。兩個不同的對象打印屬性相同的值

編輯:下面我有兩個相關的類和相關的方法。問題在於Board類的鄰居方法。看起來,當我在鄰居方法中創建一個電路板時,它並沒有做它應該做的事情。

Board類

public final class Board { 
private final int dimen; 
private final int[][] blocks; 

public Board(int[][] blocks)   // construct a board from an N-by-N array of blocks 
    // (where blocks[i][j] = block in row i, column j) 
{ 
    this.dimen = blocks.length; 
    this.blocks = new int[dimen][dimen]; 
    for (int i = 0; i < dimen; ++i) { 
     for (int j = 0; j < dimen; ++j) { 
      this.blocks[i][j] = blocks[i][j]; 
      System.out.println (this.blocks[i][j]); 
     } 
    } 
... 
... 

public Iterable<Board> neighbors()  // all neighboring boards 
{ 
    Stack<Board> neighborStack = new Stack <Board>(); 
    int x = 0, y = 0; 
    outer : for (int i = 0; i < dimen; ++i){ 
     for (int j = 0; j < dimen; ++j) { 
      if (this.blocks[i][j] == 0) { 
       x = i; 
       y = j; 
       break outer;   
      } 
     } 
    } 

    if (x == 0) 
    { 
     if (y == 0) { 
      int tmpBlocks1[][] = Arrays.copyOf (this.blocks, this.blocks.length); 
      int tmpBlocks2[][] = Arrays.copyOf (this.blocks, this.blocks.length); 

      tmpBlocks1[0][0] = tmpBlocks1[0][1]; 
      tmpBlocks1[0][1] = 0; 

      tmpBlocks2[0][0] = tmpBlocks2[1][0]; 
      tmpBlocks2[1][0] = 0; 

      Board tmpBoard1 = new Board (tmpBlocks1); 
      neighborStack.push (tmpBoard1); 

      Board tmpBoard2 = new Board (tmpBlocks2);    
      neighborStack.push (tmpBoard2); 

} 

求解器類:

public final class Solver { 

private MinPQ <SearchNode> pqOriginal; 
private MinPQ <SearchNode> pqTwin; 
Stack <Board> shortestBoardSequence = null; 
int moves = 0; 

public Solver(Board initial)   // find a solution to the initial board (using the A* algorithm) 
{ 
    pqOriginal = new MinPQ<SearchNode>(); 
    pqTwin = new MinPQ<SearchNode>(); 
    pqOriginal.insert(new SearchNode (moves, initial, null)); 
    pqTwin.insert(new SearchNode (moves, initial.twin(), null)); 
} 

public boolean isSolvable()    // is the initial board solvable? 
{ 
    SearchNode originalNode = null; 
    SearchNode twinNode = null; 
    Stack <Board> neighborBoards = null; 
    while (!pqOriginal.isEmpty() || !pqTwin.isEmpty()) { 

     originalNode = pqOriginal.delMin(); 
     // shortestBoardSequence.push(originalNode.board); 

     neighborBoards = (Stack<Board>)originalNode.board.neighbors(); 
... 
} 
... 
} 
... 
public static void main(String[] args) // solve a slider puzzle (given below) 
{ 
    // create initial board from file 
    In in = new In(args[0]); 
    int N = in.readInt(); 
    int[][] blocks = new int[N][N]; 
    for (int i = 0; i < N; i++) 
     for (int j = 0; j < N; j++) 
     blocks[i][j] = in.readInt(); 
    Board initial = new Board(blocks); 

    // solve the puzzle 
    Solver solver = new Solver(initial); 

    // print solution to standard output 
    if (!solver.isSolvable()) // SEE THE ISSOLVABLE 
     StdOut.println("No solution possible"); 
    .. 
} 
+0

您能否提供更多詳細資料給我們,以便我們進一步幫助您? – Sednus

+0

你想要什麼輸出,你得到了什麼?您需要向我們展示..可能您的'tmpBlocks1 [0] [1]'和'tmpBlocks2 [1] [0]'包含相同的值.. –

回答

4

這可能是因爲你有一個二維數組,你只複製一個維度。

這樣做:

int tmpBlocks1[][] = Arrays.copyOf (blocks, blocks.length); 

你實際上只複製行的引用,而不是行數據。你是做等價的:

int[] row0 = {0,0,0}; 
int[] row1 = {0,0,0}; 
int[] row2 = {0,0,0}; 
int[][] blocks = {row0, row1, row2}; 
int[][] tmpBlocks1 = {row0, row1, row2}; 

tmpBlocks持有相同的行爲blocks

你需要做的是數組的所謂deep copy

實施例的代碼來演示該問題:

public final class Test<T> { 
    int[][] blocks = {{0,1,2},{10,11,12},{20,21,22}}; 
    int[][] copyOfBlocks = Arrays.copyOf(blocks, blocks.length); 
    int[][] deepCopyOfBlocks = { 
    Arrays.copyOf(blocks[0], blocks[0].length), 
    Arrays.copyOf(blocks[1], blocks[1].length), 
    Arrays.copyOf(blocks[2], blocks[2].length) 
    }; 

    public void test() { 
    System.out.println("Before"); 
    System.out.println("Blocks: "+Arrays.deepToString(blocks)); 
    System.out.println("Shallow Copy: "+Arrays.deepToString(copyOfBlocks)); 
    System.out.println("Deep Copy: "+Arrays.deepToString(deepCopyOfBlocks)); 

    // Change blocks and copy and deep copy. 
    blocks[0][0] = 99; 
    copyOfBlocks[0][0] = 88; 
    deepCopyOfBlocks[0][0] = 77; 

    System.out.println("After"); 
    System.out.println("Blocks: "+Arrays.deepToString(blocks)); 
    System.out.println("Shallow Copy: "+Arrays.deepToString(copyOfBlocks)); 
    System.out.println("Deep Copy: "+Arrays.deepToString(deepCopyOfBlocks)); 
    } 

    public static void main(String[] args) throws InterruptedException { 
    try { 
     Test test = new Test(); 
     test.test(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

打印:

Before 
Blocks: [[0, 1, 2], [10, 11, 12], [20, 21, 22]] 
Shallow Copy: [[0, 1, 2], [10, 11, 12], [20, 21, 22]] 
Deep Copy: [[0, 1, 2], [10, 11, 12], [20, 21, 22]] 
After 
Blocks: [[88, 1, 2], [10, 11, 12], [20, 21, 22]] 
Shallow Copy: [[88, 1, 2], [10, 11, 12], [20, 21, 22]] 
Deep Copy: [[77, 1, 2], [10, 11, 12], [20, 21, 22]] 

注意到99的變化是由變化重寫到88說明該複製是指的行原版的。使用77時,只有深拷貝受到影響。

相關問題