[英]Passing a 2d array from one method to another
Programming language: Java 编程语言:Java
editor: Vim 编辑:Vim
I been trying to do an assignment that includs a 2d array. 我一直试图做一个包含二维数组的任务。 I've been struggling on it pretty bad since this is the second time I've used any kind of arrays. 我一直在努力,因为这是我第二次使用任何类型的阵列。 Anyway, i just got some questions that I hope someone here can clear up for me. 无论如何,我只是提出了一些问题,希望有人可以帮我解决。
1) is this is how you would pass a double array to another method? 1)这是你如何将双数组传递给另一个方法?
this is my boolean array: 这是我的布尔数组:
boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];
this is how I have been trying to pass it. 这就是我试图传递它的方式。
countNeighbors(TempGrid[][]);
this is method accepting the array: 这是接受数组的方法:
public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
however, when I compile i get an error saying: 但是,当我编译时,我得到一个错误说:
error: '.class' expected
countNeighbors(TempGrid[][]);
^
1 error
I did some research on the .class error like: 我对.class错误进行了一些研究,如:
- http://www.dreamincode.net/forums/topic/70299-class-expected-error/ - http://www.dreamincode.net/forums/topic/70299-class-expected-error/
- http://stackoverflow.com/questions/12309220/class-error-in-java-applet - http://stackoverflow.com/questions/12309220/class-error-in-java-applet
- http://www.daniweb.com/software-development/java/threads/213357/pass-2-dimensional-array-into-method - http://www.daniweb.com/software-development/java/threads/213357/pass-2-dimensional-array-into-method
and various other sites/forums. 和其他各种网站/论坛。 The solution they presented either didn't work or cause more issues with my program. 他们提出的解决方案要么不起作用,要么导致我的程序出现更多问题。
btw this is the entire method: 顺便说一下这是整个方法:
public static void genNextGrid ( boolean[][] grid )
{
boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];
TempGrid[GRIDSIZE][GRIDSIZE] = grid[GRIDSIZE][GRIDSIZE];
countNeighbors(TempGrid);
for(int row = 1; row < 18; row++)
{
countNeighbors(row);
for(int col = 1; col < 18; col++)
{
countNeighbors(col);
if(n == 3)
{
TempGrid[row][col] = true;
}
else if(n == 2 || n == 3)
TempGrid[row][col] = true;
}
else
{
TempGrid[row][col] = false;
}
}
}
}
I've attempted in removing [][]
from countNeighbors(TempGrid[][]);
我试图从countNeighbors(TempGrid[][]);
中删除[][]
countNeighbors(TempGrid[][]);
so it would look like: 所以它看起来像:
countNeighbors(TempGrid);
but gave me 3 errors 但给了我3个错误
error: method countNeighbors in class Life cannot be applied to given types;
countNeighbors(TempGrid);
^
required: boolean[][],int,int
found: boolean[][]
reason: actual and formal argument lists differ in length
error: method countNeighbors in class Life cannot be applied to given types;
countNeighbors(row);
^
required: boolean[][],int,int
found: int
reason: actual and formal argument lists differ in length
error: method countNeighbors in class Life cannot be applied to given types;
countNeighbors(col);
^
required: boolean[][],int,int
found: int
reason: actual and formal argument lists differ in length
-Thanks for your help in advance - 提前谢谢你的帮助
Yes the 2nd way is the correct way ie 是的,第二种方式是正确的方式,即
countNeighbors(TempGrid);
And thee later error is saying you the number of arguments required doesn't match, as 而后来的错误是说你所需的参数数量不匹配,如
public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
The countNeighbors
method expects 3 parameters, and you are just passing 1. Verify what you need to pass to this method, or remove them if not required. countNeighbors
方法需要3个参数,您只是传递1.验证您需要传递给此方法的内容,或者在不需要时删除它们。 Also it returns a int
, you need to assign it back to something for further use. 它还返回一个int
,你需要将它分配给某些东西以供进一步使用。 I envisage it's the variable n
, you intend to use it for. 我设想它是变量n
,你打算用它。
You need to call the method in the innermost loop as 你需要在最里面的循环中调用方法
for(int row = 1; row < 18; row++) {
for(int col = 1; col < 18; col++) {
n = countNeighbors(TempGrid,row,col);
...
}
}
You only need the [][] when you declare the variable. 声明变量时只需要[] []。 After that only use the name (ie TempGrid) 之后只使用名称(即TempGrid)
So if your method takes a 2D array and is named foo, then: foo(TempGrid)
will pass a reference to TempGrid to foo. 因此,如果您的方法采用2D数组并命名为foo,则: foo(TempGrid)
将对foo(TempGrid)
的引用传递给foo。
In Java all is object, thus your method must be declared inside a class. 在Java中,所有都是对象,因此必须在类中声明您的方法。
IMHO your method should look something like this: 恕我直言你的方法应该是这样的:
public static void genNextGrid ( boolean[][] grid )
{
boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];
TempGrid = grid;
for(int row = 1; row < 18; row++)
{
for(int col = 1; col < 18; col++)
{
int n = countNeighbors(TempGrid, row, col);
if(n == 3)
{
TempGrid[row][col] = true;
}
else if(n == 2 || n == 3)
TempGrid[row][col] = true;
else
{
TempGrid[row][col] = false;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.