Java main中的数组引用到main外部的数组

Java main中的数组引用到main外部的数组,java,arrays,reference,Java,Arrays,Reference,我试图在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++) { fo

我试图在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 
    }
}
公共类网格{
public void createGrid(){
int N=StdIn.readInt();
int thisarray[]]=新int[N][N];
对于(int x=0;x
你在寻找这个答案吗?我已将方法的返回类型更改为
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();
    }
}

将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(); 
} 
}
公共类网格
{
public int[][]createGrid(){
int N=StdIn.readInt();
int thisarray[]]=新int[N][N];
对于(int x=0;x
使
此数组
成为类的
静态
实例(因为它在
静态主
中使用)

公共类网格{
私有静态int[][]thisarray;
public void createGrid(){
int N=StdIn.readInt();
thisarray=newint[N][N];
对于(int x=0;x
添加
返回此数组
createGrid()
方法中(同时将返回类型从
void
更改为
int[][]
),主要有:

int [][] newArray = g.createGrid()
公共类网格{
//在此声明您的数组
私有int thisarray[]];
public void createGrid(){
int N=StdIn.readInt();
此数组[][]=新整数[N][N];
对于(int x=0;x
}

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; 
    } 
}
int [][] newArray = g.createGrid()
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; 
    }