0

我正在尝试创建 myBoard[][] 的克隆,因为现在当我尝试返回它时,我得到的所有错误值都是不正确的。

我将在哪里克隆它,以及如何克隆它,以便获得具有合法值而不是全部 false 的 myBoard[][] 的副本?我正在尝试使用 public boolean[][] getBoard() 在底部返回它

package model;

public class NQueensModel
{
    private int myNumsQueen;
    public int myPossibilities=0;
    private boolean[][] myBoard;
    private boolean[][] myGridBoard;

    public static void main (String[] args) {
        //...
    }
    public NQueensModel(int nQueens)
    {
        myNumsQueen = nQueens;
        myPossibilities=0;
        myBoard = new boolean[myNumsQueen][myNumsQueen];
    }
    public boolean solvePuzzle()
    {
        return solvePuzzle(0);            
    }
    private boolean solvePuzzle(int ncolumn)
    {
        if(ncolumn>myNumsQueen-1)
        {               
            myPossibilities++;
        }
        int i;       
        for( i =0; i<myNumsQueen;i++)
        {           
            if(this.isSafeMove(i, ncolumn)==true)
            {                
                this.placeQueen(i,ncolumn);
                if(this.solvePuzzle(ncolumn+1)==true)
                {                           
                    return true;                          
                }
                this.removeQueen(i, ncolumn);
            }                     
        }                  
        return false;            
    }

    private boolean doIt(int county)
    {
        if(county>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private boolean isSafeMove(int row, int col)
    {
        if(row <0 || row>=myNumsQueen || col<0 || col>=myNumsQueen)
        {
            return false;
        }
        else if(this.checkLowerDiag(row, col)==true ||this.checkUpperDiag(row, col)==true ||this.checkLeft(row,col)==true)
         {
             return false;
         }
         else
         {              
            return true;
         }             
    }

    private boolean checkUpperDiag(int row, int col)
    {    
        if(row==0)
        {
            return false;
        }
        else
        {
            for(int i=row, j = col; i>=0 && j>=0; i--, j--)
            {
                if(myBoard[i][j]==true)
                {
                    return true;
                }
            }
            return false;
        }
    }

    private boolean checkLowerDiag(int row, int col)
    {            
        if(col==0 )
        {           
            return false;             
        }
        if(row==myNumsQueen-1){
            return false;
        }
        else
        {                
            for(int i = row, j = col; i<myNumsQueen && j>=0;  i++, j--)
            {   
                if(j>=myNumsQueen)
                {
                    return false;
                }                   
                else if(myBoard[i][j]==true)
                {
                    return true;                        
                }                                       
            }
            return false;
        }
    }

    private boolean checkLeft(int row, int col)
    {
        if(col==0)
        {
            return false;
        }
        else 
        {
            for(int i = col; i>=0; i--)
            {                   
                if(i>=myNumsQueen)
                {
                    return false;
                }

                else if(myBoard[row][i]==true)
                {
                    return true;
                }
            }
            return false;
        }          
    }
    private boolean placeQueen(int row, int col)
    {
        if(col>=myNumsQueen)
        {
            return false;
        }

        myBoard[row][col] = true;
        return true;
    }
    private boolean removeQueen(int row, int col)
    {
        myBoard[row][col] = false;
        return false;
    }
    public int getPossibilities(){
        return myPossibilities;
    }
    public boolean[][] getBoard()
    {
        for(int i  = 0; i<5; i++)
        {               
            for(int j = 0; j<5; j++)
            {
                System.out.println("myBoard : " +myBoard[i][j]);
            }
        }           
        return myBoard;
    }
}
4

2 回答 2

1

好吧,如果您只是想创建一个克隆数组,则必须通过迭代旧数组并将值复制过来来构建它:

for (int i = 0; i < oldArray.length; ++i)
  for (int j = 0; j < oldArray[0].length; ++j)
    newArray[i][j] = oldArray[i][j];
于 2013-11-11T23:50:06.700 回答
1

当您使用本机类型时,您可以使用clone().

public static boolean[][] clone2DArray(boolean [][] array){
     // this clone is a shallow copy
     boolean[][] newArray =(boolean[][]) array.clone();
    //now you need to clone each array cause still referring to the same
    for(int i=0;i<array.length;i++){
        newArray[i]=Arrays.copyOf(array[i],array[i].length); 
        //newArray[i]=(boolean[])array[i].clone(); this is valid too           
    }
       return newArray;
}

在Arrays#copyOf()中阅读更多内容

于 2013-11-12T00:05:57.787 回答