How to use for elements of a 2D array?
I started a 2D array with a class board
, and type cell
. Basically, I want to use cell elements, and I want to use methods of that category. However, I am unsure of how to implement it, because I get an error while trying out
board [1] [1] .cellmethod ()
< P> The code for the board: Public square board {Private int col = 1, line = 1; Private cell [] [] Board; Private RandomNumberGenerator Rand = New RandomNumberGenerator (); Public Board () {Board = New Cell [Call] [Line]; Start the board with cells for (int r = 0; r & lt; = rows; r ++) {for (int c = 0; c
cell class
public class cell {// which will include a private image in cell size; // where the cell cell number is located offset - & gt; Need to translate the given coordinates into pixel private int x, y; Private Indus SHAPE_WIDTH = 50; // Width of each shape (pixels) Private Inc. SHAPE_HEIGHT = 50; // the height of each pixel; Private boolean visible; Public cell (integer figure, 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;}}
where I call the board object
The public class extends to the PlayState State {Private Board Board; @ Override Public Wide Init () {Board = New Board (); } @ Override Public Zero Update (Float Delta) {} @ Override Public Empty Render (Painter G) {for (int r = 0; r & lt; = board.getrev (); r ++} {for (int c = 0; c & lt; = Board.getCol (); c ++) {board [0] [0] // error, any cell methods can not be applied}}}
Your board
array size is one (row and column).
private int col = 1, line = 1; Therefore, there is only one element available on your board
in the board [0] [0]
, the first row and the first column. Throws an ArrayIndexOutOfBoundsException
from reaching the board [1] [1]
. Remember that an array
index can be maximized values of the array
board = new board ();
board
is not an array; If this is a board
object, obviously you can not see it with the index [] []
. You need to expose the underlying board [] []
via the Gether method.
public cell [] [] getBoard () {return board; }
You can then use the recipient in the override public zero render form as your render ()
method
@ Are (illustrator g) {cell [] [] boardarr = board.eatbauer (); For (int r = 0; r <= board.getRow (); r ++) {for (int c = 0; c & lt; = board.getCol (); c ++) {boardArr [ R] [c] .cellMethod (); }}}
No comments:
Post a Comment