简体   繁体   中英

Initialize 2D Array of custom objects

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 ?

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) .

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
    }
}

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.

First variation

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();
    }
}

Second variation

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM