Reputation: 79
I've a question about initializing a 2D Array of custom objects.
I have 2 Objects : a CellEntity
, and a MapEntity
who contains:
private final ICellEntity[][] cellMap;
I've initialized cellmap
in the MapEntity
's constructor
cellMap = new CellEntity[width][length];
but every CellEntity
is null
.
I want to know if there's a solution to invoke (to force) a method in CellEntity
class in order to init each CellEntity
in the cellMap
?
Upvotes: 2
Views: 29541
Reputation: 17454
I want to know if there's a solution to invoke (to force) a method in CellEntity class in order to init each CellEntity in the cellMap?
It is actually possible, but the array has to be created first.
Create array in constructor first. We cannot change the reference of cellMap after creation, but we still can assign the values within it:
class TestRunner{
public static void main(String[] args){
MapEntity me = new MapEntity(5, 5);
me.initCellMap(); //init cellMap separately
}
}
class MapEntity
{
private final CellEntity[][] cellMap;
public MapEntity(int width, int length){
cellMap = new CellEntity[width][length]; //has to be done in constructor
}
public void initCellMap(){
for(int x=0; x<cellMap.length; x++)
for(int y=0; y<cellMap[0].length; y++)
cellMap[x][y] = new CellEntity();
}
}
Almost similar to the first, you create the array first, if you do not want to create it in the constructor (personallyy, I do not favour this approach):
class TestRunner{
public static void main(String[] args){
MapEntity me = new MapEntity();
me.initCellMap(); //init cellMap separately
}
}
class MapEntity
{
final int LENGTH = 5;
final int WIDTH = 5;
final CellEntity[][] cellMap = new CellEntity[WIDTH][LENGTH];
public MapEntity(){
}
public void initCellMap(){
for(int x=0; x<cellMap.length; x++)
for(int y=0; y<cellMap[0].length; y++)
cellMap[x][y] = new CellEntity();
}
}
Upvotes: 0
Reputation: 17454
I dont want to modify the cellMap it's the reason why cellMap is final
Since you want to make it final. You can set it's value via the constructor:
class MapEntity
{
private final ICellEntity[][] cellMap;
public MapEntity(ICellEntity[][] cellMap){
this.cellMap = cellMap;
}
}
You can create an initialized cellMap array first, then pass it via the constructor to set the cellMap's value within MapEntity.
//Initialize your cellMap else where first
ICellEntity[][] cellMap = new CellEntity[width][length];
for(int x=0; x<width; x++)
for(int y=0; y<length; y++)
cellMap[x][y] = new CellEntity();
//Pass in the initialized cellMap via the constructor
MapEntity mapEntity = new MapEntity(cellMap);
I want to know if there's a solution to invoke (to force) a method in CellEntity class in order to init each CellEntity in the cellMap?
Well, if your cellMap
was declared as final, there is no way you can set it via a method (accessor), other than probably using reflection (which I don't think you will like it very much).
Upvotes: 4
Reputation: 2492
cellMap = new CellEntity[width][length];
for(int i = 0; i < width; i++){
for(int j = 0; j < length; j++){
cellMap[i][j] = new CellEntity(); //do constructor-y stuff here
}
}
Upvotes: 0