编程语言:Java
编辑:Vim
我一直在尝试做一个包含二维数组的任务。我一直在苦苦挣扎,因为这是我第二次使用任何类型的数组。无论如何,我只是有一些问题,我希望这里有人可以为我解决。
1)这就是您将双数组传递给另一个方法的方式吗?
这是我的布尔数组:
boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];
这就是我一直试图通过它的方式。
countNeighbors(TempGrid[][]);
这是接受数组的方法:
public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
但是,当我编译时,我收到一条错误消息:
error: '.class' expected
countNeighbors(TempGrid[][]);
^
1 error
我对 .class 错误进行了一些研究,例如:
- http://www.dreamincode.net/forums/topic/70299-class-expected-error/
- 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
和各种其他网站/论坛。他们提出的解决方案要么不起作用,要么导致我的程序出现更多问题。
顺便说一句,这是整个方法:
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;
}
}
}
}
我试图从中删除[][]
,countNeighbors(TempGrid[][]);
所以它看起来像:
countNeighbors(TempGrid);
但给了我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
-提前感谢您的帮助