我正在尝试将我的char数组grid1传递给名为status的方法。我收到错误char无法转换为char [] []。我如何传递grid1以便在for循环中工作?
for (int row = 0; row < 30; row++){
for (int col = 0; col < 30; col ++){
if (status(grid1[row][col], row, col)){
}
}
}
public boolean status(char [][] grid, int a, int b){
char value = grid[a][b];
if (value == 'X'){
//add X to another array
return true;
} else {
return false;
}
//add - to another array
}
答案 0 :(得分:1)
问题是您的方法签名期望一个数组,并使用数组中的值调用它。
调用如:
status(grid1, row, col)
或修复方法签名
public boolean status(char grid){
char value = grid;
答案 1 :(得分:0)
只需替换
if (status(grid1[row][col], row, col)){
与
if (status(grid1, row, col)){
答案 2 :(得分:0)
grid1[row][col]
是char
数组的单个元素,因此属于char
类型。您需要传递整个数组grid1
。
答案 3 :(得分:0)
您已经在循环中取消引用grid1数组。因此,除非因为某种原因将该循环作为状态方法的一部分,否则您的状态方法可能会简单得多:您不需要传递char[][]
,您可以传递char
。显然,您不需要列和行索引。你的方法应该是这样的:
public boolean status(char value){
if (value == 'X'){
//add X to another array
return true;
} else {
return false;
}
//add - to another array
}