如何在我的 getCell 方法中访问 Cell Array?(爪哇) [英] How can I access the Cell Array in my getCell method? (Java)

查看:43
本文介绍了如何在我的 getCell 方法中访问 Cell Array?(爪哇)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是实现康威的生命游戏.因此我需要创建类 GameMap.在这个类中,我将初始化一个二维数组.所以我用这两种方法.

my Task is to make an implementation of Conway's Game of Life. Therefor I need to create the class GameMap. In this class I will initialize an 2D Array. 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);
    }

现在我想访问 cellArray 以使用 getCell(int posX, int posY) 方法访问一个特殊的 Cell.我的问题是如何访问 cellArray?我想像这样访问它:

Now I want to access the cellArray to access a special Cell with the getCell(int posX, int posY) method. My question is how I can access the cellArray? I wanted to access it like this:

public Cell getCell(int posX, int posY){
        return cellArray[posX][posY];
    }

这样我就可以将 Cell 放在一个特殊的位置.希望有人能帮帮我.

So that I get the Cell at a special position. 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];
    }
}

IDE 说 getCell 方法中的 cellArray 不是变量.

And the IDE says that cellArray in the method getCell is not a variable.

推荐答案

IDE 说 cellArray 不能解析为变量,因为它是局部变量,要通过这个问题只需移动 Cell[][] cellArray = new Cell[width][height];buildCellArray()之外.

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]; outside the 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];
    }
}

这篇关于如何在我的 getCell 方法中访问 Cell Array?(爪哇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆