简体   繁体   English

如何在我的 getCell 方法中访问元胞数组? (爪哇)

[英]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.

相关问题 JAVA:如果我有一个包含数组的类,该如何在我的main方法中访问该数组? - JAVA: If I have a class that contains an array, how would I access that array in my main method? 为什么我不能从另一个方法访问我在 main 方法中创建的 int 数组值? (初学者Java编码器) - Why can't I access my int array values that I made in my main method from another method? (beginner Java coder) 我如何访问我的方法(Java)? - How do I access my method (Java)? 如何在主方法中访问布尔数组的辅助方法? - How can I access a helper method for a boolean array in my main method? 从我的 Java 应用程序调用 getQueryResults 方法时,如何从 QueryResponse 访问架构? - How can I access Schema from the QueryResponse while calling getQueryResults method from my Java application? 如何从非静态方法访问作为我的实例变量的对象数组? - How can i access array of objects which is my instance variable from a non static method? 如何在 java (android Studio) 中填充数组的一个单元格 - How can i fill one cell of array in java (android Studio) 如何从 Java 中的另一个方法访问变量? - How can I access a variable from another method in Java? java - 如何从Java中的另一个方法访问对象? - How can i access an object from another method in java? 如何从数组访问数据以在方法中使用它 - how to can i access data from array to use it in method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM