[英]How can I access the Cell Array in my getCell method? (Java)
my Task is to make an implementation of Conway's Game of Life.我的任务是实现康威的生命游戏。 Therefor I need to create the class GameMap.
因此我需要创建 class GameMap。 In this class I will initialize an 2D Array.
在这个 class 中,我将初始化一个二维数组。 Therefor I use those two methods.
因此我使用这两种方法。
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
Now I want to access the cellArray to access a special Cell with the getCell(int posX, int posY) method.现在我想访问 cellArray 以使用 getCell(int posX, int posY) 方法访问一个特殊的 Cell。 My question is how I can access the cellArray?
我的问题是如何访问 cellArray? I wanted to access it like this:
我想像这样访问它:
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
So that I get the Cell at a special position.这样我就可以在特殊的 position 处获得 Cell。 I hope somebody can help me out.
我希望有人可以帮助我。
So the complete code part is:所以完整的代码部分是:
public class GameMap {
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
And the IDE says that cellArray in the method getCell is not a variable. IDE 表示方法 getCell 中的 cellArray 不是变量。
The IDE says cellArray
cannot be resolve to a variable because it is local variable, to pass this problem just move Cell[][] cellArray = new Cell[width][height];
IDE 说
cellArray
无法解析为变量,因为它是局部变量,要通过此问题只需移动Cell[][] cellArray = new Cell[width][height];
outside the buildCellArray()
.在
buildCellArray()
之外。
public class GameMap {
Cell[][] cellArray;
private static Cell[][] buildCellArray(int width, int height){
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.