|
Re: 2 dimension array of objects.how do i initialize values;
|
Posted: Dec 6, 2003 4:50 PM
|
|
>for (int i=0;i<size;i++) >for (int j=0;j<size;j++) >cell[j].hasObstacle=false; //???
When you created call[][] then all elements are initailized to null. After creating the array, first thing you need to do it to create a new Cell object and assign it to each element. You can do it as:
for (int i=0;i<size;i++){
for (int j=0;j<size;j++) {
cell[i][j]= new Cell(); // Create and assign new Cell object
}
}
Since hasObstacle is boolean instance variable for Cell object, its value will be set to false by default. So,each cell value for hasObstacle read as
cell[i][j].hasObstacle
will be false after the above for loop is executed.
|
|