java - values in LinkedList changed outside of the function, clone() a 2D arrays -
if try read linkedlist “solvedbords”outside solve function values in 2d array “board” saved in solvedbords turns 0.
but in “solved” function values in in 2d array “board” saved in solvedbords entirely accurate.
is due recursion? explanation appreciated
main class
public class main { public static void main(string[] arg){ testqueen n = new testqueen(); } }
test class
import javax.swing.*; import java.util.linkedlist; public class testqueen { private int[][] board; private linkedlist<int[][]> solvedbords = new linkedlist<>(); private static int boardsize = 0; testqueen() { boardsize = 8; board = new int[boardsize][boardsize]; start(); } void start() { solve(0); system.out.println("solvedbords = " + solvedbords.size()); while (!solvedbords.isempty()) { system.out.println("this in start funktion"); printbord(solvedbords.pop()); system.out.println(""); } } void solve(int row) { if (row == boardsize) { system.out.println("this in solve function: "); printbord(board); system.out.println(""); solvedbords.add(board.clone()); // 2d array “board” saved in // solvedbords /* * system.out.println("solvedbords = " + solvedbords.size()); * while(!solvedbords.isempty()){ printbord(solvedbords.pop()); * system.out.println(""); } */ return; } (int colum = 0; colum < boardsize; colum++) { if (validmove(row, colum)) { pacequeen(row, colum, 0); solve(row + 1); pacequeen(row, colum, 1); } } } boolean validmove(int x, int y) { (int = 0; < boardsize; i++) { if (get(x, i) > 0) { return false; } if (get(i, y) > 0) { return false; } if (get(x - i, y - i) > 0) { return false; } if (get(x - i, y + i) > 0) { return false; } if (get(x + i, y - i) > 0) { return false; } if (get(x + i, y + i) > 0) { return false; } } return true; } /** * * if type == 1 queen going placed in row x , column y * * else if type == 0 queen going removed row x column y * * @param x * row * @param y * column * @param type * 0 or 1 */ void pacequeen(int x, int y, int type) { if (type == 0) { board[x][y] = 1; } else if (type == 1) { board[x][y] = 0; } } int get(int x, int y) { if (x < 0 || y < 0 || x >= boardsize || y >= boardsize) { return -1; } return board[x][y]; } void printbord(int[][] board) { (int = 0; < boardsize; i++) { (int j = 0; j < boardsize; j++) { if (board[i][j] > 0) { system.out.print("[1]"); } else if (board[i][j] == 0) { system.out.print("[0]"); } } system.out.println(); } } }
the problem can't use clone()
2d arrays.
in java, int[][] board
not 2d array or matrix - 1d array of 1d arrays. in other words, board
array of object references, each object 1d int[]
array. when call clone()
on array[][]
performs shallow copy - outer array cloned, inner arrays not - new references same data. upshot of every time change values in board
changing in solvedboards
well.
one solution write deep copy function clone2darray
, replace call clone()
clone2darray()
:
int[][] clone2darray(int[][] original) { int [][] cloned = new int[original.length][]; (int = 0; < original.length; i++) { cloned[i] = original[i].clone(); } return cloned; } void solve(int row) { ... solvedbords.add(clone2darray(board)); // 2d array “board” saved ... }
another, perhaps better, solution might create matrix class store board.
also aware recursively calling solve()
, when return call, board
have changed. might not expecting. instead pass board
in parameter solve
, making sure pass in clone2darray()
copy. ensure board
not changed when return recursive call.
Comments
Post a Comment