java - An array in main reffering to an array outside main -
i'm trying create array in main making identical array created in method outside main. cant think of way this...
here's code:
public class grid { public void creategrid() { int n = stdin.readint(); int thisarray[][] = new int[n][n]; (int x = 0; x < n; x++) { (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 } }
are looking answer? have changed return type of method int[][]
, inside main method can call creategrid
method int[][]
public class grid { public int[][] creategrid() { int n = stdin.readint(); int thisarray[][] = new int[n][n]; (int x = 0; x < n; x++) { (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(); } }
if want newarray
on basis of n
send n
creategrid
method. have add 1 parameter in creategrid
method given below. remove stdin.readint()
creategrid
method
public class grid { public int[][] creategrid(int n) { int thisarray[][] = new int[n][n]; (int x = 0; x < n; x++) { (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 basis of input value. } }
Comments
Post a Comment