Java LinkedList中的值在函数clone()a 2D数组之外更改

Java LinkedList中的值在函数clone()a 2D数组之外更改,java,arrays,recursion,multidimensional-array,linked-list,Java,Arrays,Recursion,Multidimensional Array,Linked List,如果尝试从solve函数外部的LinkedList“solvedBords”中读取,则保存在solvedBords中的2d数组“board”中的所有值都将变为0 但在“solved”函数中,保存在solvedBords中的二维数组“board”中的所有值都是完全准确的 这是因为递归吗?如有任何解释,将不胜感激 主类 public class main { public static void main(String[] arg){ testQueen N = new test

如果尝试从solve函数外部的LinkedList“solvedBords”中读取,则保存在solvedBords中的2d数组“board”中的所有值都将变为0

但在“solved”函数中,保存在solvedBords中的二维数组“board”中的所有值都是完全准确的

这是因为递归吗?如有任何解释,将不胜感激

主类

public class main {

   public static void main(String[] arg){
      testQueen N = new testQueen();
   }
}
测试班

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 is in the start funktion");
            printBord(solvedBords.pop());
            System.out.println("");
        }
    }

    void solve(int row) {

        if (row == boardSize) {
            System.out.println("this is in the solve function: ");
            printBord(board);
            System.out.println("");
            solvedBords.add(board.clone()); // 2d array “board” that is saved in
                                            // solvedBords
            /*
             * System.out.println("solvedBords = " + solvedBords.size());
             * while(!solvedBords.isEmpty()){ printBord(solvedBords.pop());
             * System.out.println(""); }
             */
            return;
        }

        for (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) {
        for (int i = 0; i < 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 the queen is going to be placed in row x and column y
     *
     * else if type == 0 the queen is going to be removed from row x column y
     *
     * @param x
     *            is the row
     * @param y
     *            is the column
     * @param type
     *            is 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) {

        for (int i = 0; i < boardSize; i++) {
            for (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();
        }
    }
}
import javax.swing.*;
导入java.util.LinkedList;
公共类测试皇后{
私人int[]董事会;
私有LinkedList solvedBords=新LinkedList();
私有静态int boardSize=0;
testQueen(){
木板尺寸=8;
board=新int[boardSize][boardSize];
start();
}
void start(){
求解(0);
System.out.println(“solvedBords=“+solvedBords.size());
而(!solvedBords.isEmpty()){
System.out.println(“这是在启动函数中”);
printBord(solvedBords.pop());
System.out.println(“”);
}
}
无效解算(整数行){
如果(行==板尺寸){
System.out.println(“这在solve函数中:”);
printBord(董事会);
System.out.println(“”);
solvedBords.add(board.clone());//保存在中的二维数组“board”
//溶词
/*
*System.out.println(“solvedBords=“+solvedBords.size());
*而(!solvedBords.isEmpty()){printBord(solvedBords.pop());
*System.out.println(“”;}
*/
返回;
}
对于(int column=0;column0){
返回false;
}
如果(得到(i,y)>0){
返回false;
}
如果(得到(x-i,y-i)>0){
返回false;
}
如果(得到(x-i,y+i)>0){
返回false;
}
如果(得到(x+i,y-i)>0){
返回false;
}
如果(得到(x+i,y+i)>0){
返回false;
}
}
返回true;
}
/**
*
*如果type==1,则皇后将被放置在第x行和第y列中
*
*否则,如果type==0,则皇后将从第x行第y列中删除
*
*@param x
*这是一排吗
*@param y
*这是专栏
*@param类型
*是0还是1
*/
void paceQueen(整数x、整数y、整数类型){
如果(类型==0){
董事会[x][y]=1;
}else if(类型==1){
板[x][y]=0;
}
}
int get(int x,int y){
如果(x<0 | | y<0 | | x>=boardSize | | y>=boardSize){
返回-1;
}
返回板[x][y];
}
无效打印端口(int[][]板){
对于(int i=0;i0){
系统输出打印(“[1]”);
}else if(板[i][j]==0){
系统输出打印(“[0]”);
}
}
System.out.println();
}
}
}

问题是不能将
clone()
用于二维阵列

在Java中,
int[]board
不是真正的二维数组或矩阵,它是由一维数组组成的一维数组。换句话说,
board
是一个对象引用数组,其中每个对象都是1D
int[]
数组。当您在
数组[][]
上调用
clone()
时,它会执行浅层复制-外部数组被克隆,但内部数组不是-它们只是对相同数据的新引用。这样做的结果是,每次更改
board
中的值时,您也在更改
solvedBoards
中的所有内容

一种解决方案是编写深度复制函数
clone2dArray
,并将对
clone()
的调用替换为
clone2dArray()

int[]]clone2dArray(int[]]original){
int[][]克隆=新int[original.length][];
对于(int i=0;i
另一个可能更好的解决方案可能是创建一个矩阵类来存储您的电路板


还要注意,您正在递归调用
solve()
,当您从调用返回时,
board
将发生更改。这可能不是你所期望的。您可以将
board
作为参数传递给
solve
,确保始终将其作为
clone2dArray()
副本传递。将确保当您从递归调用返回时,
不会更改。

您能修复缩进吗?我已尽力修复,粘贴代码时遇到问题。您的帖子不可读,请使用标点符号和正确的语法。
int[][] clone2dArray(int[][] original) {
    int [][] cloned = new int[original.length][];
    for (int i = 0; i < original.length; i++) {
        cloned[i] = original[i].clone();
    }
    return cloned;
}

void solve(int row) {
    ...
    solvedBords.add(clone2dArray(board)); // 2d array “board” that is saved 
    ...
}