如何创建二维数组的克隆?

时间:2013-11-11 23:45:10

标签: java function methods boolean

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

我究竟会在哪里克隆它,以及如何使用合法值而不是全部错误来获取myBoard [] []的副本?我试图在公共布尔[] [] 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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

for (int i = 0; i < oldArray.length; ++i)
  for (int j = 0; j < oldArray[0].length; ++j)
    newArray[i][j] = oldArray[i][j];

答案 1 :(得分: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()

中了解详情