如何使用2d数组元素的方法?

时间:2015-03-17 04:55:10

标签: java arrays object

如何使用2d数组元素的方法?

我有一个班级board,并初始化了一个类型为cell的二维数组。基本上,我想使用单元格元素,并使用该类中的方法。 但是,我不确定如何实现它,因为我在尝试

时遇到错误
board[1][1].cellmethod()

董事会代码:

public class Board {
    private int col = 1, row= 1;
    private cell[][] board;
    private RandomNumberGenerator rand = new RandomNumberGenerator();


    public Board(){
        board = new cell[col][row];
    //Initialize board with cells
        for (int r = 0 ; r<=row; r++){
            for(int c = 0; c<= col; c++){
                board[c][r] = new cell(rand.getRandIntBetween(1,6), translateOffsetToPixel(c,r).getX(), translateOffsetToPixel(c,r).getY());
            }
        }
    }

CELL CLASS

public class cell {
    //which shape the cell will consist
    private int shape;

    //offset of where the cell is located by cell number -> need to translate the given coordinates to pixel
    private int x, y;

    private int SHAPE_WIDTH = 50;   //Width of each shape (pixels)
    private int SHAPE_HEIGHT = 50;  //Height of each shape (pixels)

    private Rect rect;
    private boolean visible;

    public cell(int shape, int x, int y){
        this.shape = shape;
        this.x = x;
        this.y = y;
        rect = new Rect( x, y, x + SHAPE_WIDTH, y + SHAPE_HEIGHT);
        visible = false;
    }

    public int getX() {return x;}
    public int getY() {return y;}
    public int getShape() {return shape;}



}

我在哪里召集董事会对象

public class PlayState extends State{
    private Board board;


    @Override
    public void init() {
        board = new Board();

    }

    @Override
    public void update(float delta) {

    }

    @Override
    public void render(Painter g) {
        for(int r = 0; r<=board.getRow(); r++){
            for(int c = 0; c<=board.getCol(); c++){
                board[0][0].  // ERROR, can't implement any cell methods
            }
        }

    }

3 个答案:

答案 0 :(得分:1)

您的board数组大小为1(行和列)。

private int col = 1, row= 1;

因此,您的boardboard[0][0],第一行和第一列只有一个元素可用。因此,访问board[1][1]会引发ArrayIndexOutOfBoundsException

请注意,array索引的最大值只能为array.length - 1


在实际实施中

board = new Board();

board不是数组;它是一个Board对象。因此,显然您无法使用索引[][]访问它。您需要通过getter方法公开底层的board[][]

public cell[][] getBoard() {
    return board;
}

然后,您可以将render()方法中的getter用作

@Override
public void render(Painter g) {
    cell[][] boardArr = board.getBoard();
    for(int r = 0; r<=board.getRow(); r++){
        for(int c = 0; c<=board.getCol(); c++){
            boardArr[r][c].cellMethod();
        }
    }
}

答案 1 :(得分:1)

您需要使用:

board.board[0][0].cellMethod();

虽然第一个boardBoard类的实例,board.board是指二维数组。

我使用了board.board但您可以使用getter方法访问它,如果您需要将其保密。

答案 2 :(得分:0)

如果数组大小为1x1,则只能在[0] [0]处存储一个元素。我已经在您的代码中为您更改了数组大小,请尝试使用此代码,看看是否有效。

董事会代码:

public class Board 
{
    private int col = 50, row= 50;
    private cell[][] board;
    private RandomNumberGenerator rand = new RandomNumberGenerator();


    public Board()
    {
        board = new cell[col][row];
    //Initialize board with cells
        for (int r = 0 ; r<=row; r++)
        {
            for(int c = 0; c<= col; c++)
            {
                board[c][r] = new cell(rand.getRandIntBetween(1,6), translateOffsetToPixel(c,r).getX(), translateOffsetToPixel(c,r).getY());
            }
        }
    }

只是一个快速提示

另外,我发现,如果将括号放在函数之后的下一行,以获得更加对齐的外观,则可以使代码的可读性更容易。像这样(我也相应地修改了你的代码):

int fibonacci(int n)
{
    //code...
}