0

我试图通过使其与在我的 main 之外的方法中创建的数组相同来在我的 main 中创建一个数组。我想不出任何办法来做到这一点...

这是我的代码:

public class GRID {     
    public void createGrid() {
        int N = StdIn.readInt();
        int thisarray[][] = new int[N][N];
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                int n = (int) (Math.random() * 6 + 1);
                thisarray[x][y] = n;                
            }
        }

    }
    public static void main(String []args){     
        GRID g = new GRID();
        int [][] newArray = //thisarray 
    }
}
4

5 回答 5

1

你在寻找这个答案吗?我已将方法的返回类型更改为int[][] main 方法内部,我们可以调用createGrid方法来获取int[][]

public class GRID {     
    public int[][] createGrid() {
        int N = StdIn.readInt();
        int thisarray[][] = new int[N][N];
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                int n = (int) (Math.random() * 6 + 1);
                thisarray[x][y] = n;                
            }
        }
        return thisarray;

    }
    public static void main(String []args){     
        GRID g = new GRID();
        int [][] newArray = g.createGrid();
    }
}

如果你想newArray基于N然后发送NcreateGrid方法。为此,您必须在createGrid方法中添加一个参数,如下所示。StdIn.readInt()createGrid方法中删除

public class GRID {     
    public int[][] createGrid(int N) {
        int thisarray[][] = new int[N][N];
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                int n = (int) (Math.random() * 6 + 1);
                thisarray[x][y] = n;                
            }
        }
        return thisarray;

    }
    public static void main(String []args){     
        GRID g = new GRID();
        int [][] newArray = g.createGrid(StdIn.readInt());  //new array everytime on the basis of input value.
    }
}
于 2013-04-09T09:54:15.753 回答
0

将 createGrid() 的返回类型从 void 更改为 int[][]

public class GRID 
{

public int[][] createGrid() {
    int N = StdIn.readInt();
      int thisarray[][] = new int[N][N];
    for (int x = 0; x < N; x++) {
        for (int y = 0; y < N; y++) {
            int n = (int) (Math.random() * 6 + 1);
            thisarray[x][y] = n;
        }
    }
   return thisarray;
}
public static void main(String []args)
{
   GRID g = new GRID();
   int [][] newArray = g.createGrid(); 
} 
}
于 2013-04-09T09:54:45.093 回答
0

thisarray创建一个static类的实例(因为它在 中使用static main

public class GRID {

    private static int[][] thisarray;

    public void createGrid() {
        int N = StdIn.readInt();
        thisarray = new int[N][N];
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                int n = (int) (Math.random() * 6 + 1);
                thisarray[x][y] = n;

            }
        }
    }

    public static void main(String []args){
        GRID g = new GRID();
        g.createGrid();
        int [][] newArray = thisarray; 
    } 
}
于 2013-04-09T09:55:39.787 回答
0

添加方法(也将返回类型从to更改return thisarray;为),在 main 中您将拥有:createGrid()voidint[][]

int [][] newArray = g.createGrid()
于 2013-04-09T09:55:49.013 回答
0
public class GRID {  
    //Declare your array here   
    private int thisarray[][];
    public void createGrid() {
        int N = StdIn.readInt();
        thisarray[][] = new int[N][N];
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                int n = (int) (Math.random() * 6 + 1);
                thisarray[x][y] = n;                
            }
        }

    }
    public static void main(String []args){     
        GRID g = new GRID();
        //Then you can use it in the main
        int [][] newArray = thisarray; 
    }

}

于 2013-04-09T10:03:16.087 回答