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:
Copyclass 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.
Copy//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).
Videos
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:
Copyclass 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.
Copy//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).
CopycellMap = 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
}
}
[ please don't judge me for answering my own question ]
One possible solution is using 2D ArrayLists. The way a 2D Array works is by literally having a 1D array consisting of 1D arrays.
Maybe the same concept can be applied to ArrayLists, which can in fact store any object types without having accessibility issues. A 5x5 ArrayList can be created as follows:
ArrayList<ArrayList> someArray = new ArrayList<ArrayList>();
for(int x=0 ; x<5 ; x++)
{
someArray.add(new ArrayList());
for(int y=0 ; y<5 ; y++)
someArray.get(x).add(null);
}
and to set row 1 col 2 to a cat object:
someArray.get(1).set(2, new Cat());
someArray.get(3).set(4, new Dog());
So 2D ArrayLists, though a bit confusing might solve this issue pretty efficiently.
to call the eat() method of the Dog class and `Cat`` class, You can cast the array reference to a subclass reference.((Dog)someArray[x][y]).eat();
public class Dog{
public Dog(){
super();
}
public String eat(){
String str = "yum";
return str;
}
public static void main (String[]args){
Object [][] arr = new Object [2] [2];
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
arr[i][j] = new Dog();
}
}
System.out.println(((Dog)arr[1][1]).eat());
}
}
Unless if I have miscounted your opening and closing braces, your for loop is not inside any method, it's directly in your class body. That's why you're getting unexpected token. You will probably want to move it into the Breakout constructor.
In order to create a 2D array in Java, you can create such an array like this:
Object testArray = new Object[10][10];
This array is now a 10*10 array with memory allocated for 100 Object references.
You can create pointers two Objects with a double for-loop:
for (int i = 0; i < testArray.length(); i++) {
for (int j = 0; j < testArray.length; j++) {
testArray[i][j] = Object; //here you input the objects that you want to point to
}
}