2

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?

4
  • What is ICellEntity? Is it a typo error with an extra i infront? Commented Jan 22, 2016 at 19:59
  • Any reason why you want to declare your cellMaparray as final? Commented Jan 22, 2016 at 20:02
  • public class CellEntity implements ICellEntity ; CellEntity constructor needs a CellBuilder Class in order to create the Object. I dont want to modify the cellMap it's the reason why cellMap is final. Commented Jan 22, 2016 at 20:08
  • Take a look at my solutions. If it helps, do accept the solution. Commented Jan 22, 2016 at 20:50

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

It's the only one solution ? There's no way to implements this method on CellEntity Class ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.