java 初始化自定义对象的二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34954795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:34:59  来源:igfitidea点击:

Initialize 2D Array of custom objects

javaarrays

提问by Maxime Gajovski

I've a question about initializing a 2D Array of custom objects.

我有一个关于初始化自定义对象的二维数组的问题。

I have 2 Objects : a CellEntity, and a MapEntitywho contains:

我有 2 个对象:aCellEntity和 a MapEntitywho 包含:

private final ICellEntity[][] cellMap;

I've initialized cellmapin the MapEntity's constructor

我已经cellmapMapEntity的构造函数中初始化

cellMap = new CellEntity[width][length];

but every CellEntityis null.

但每一个CellEntity都是null

I want to know if there's a solution to invoke (to force) a method in CellEntityclass in order to init each CellEntityin the cellMap?

我想知道是否有解决方案可以调用(强制)CellEntity类中的方法以便CellEntitycellMap?

回答by user3437460

I dont want to modify the cellMap it's the reason why cellMap is final

我不想修改 cellMap 这就是 cellMap 是 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.

您可以先创建一个初始化的 cellMap 数组,然后通过构造函数传递它以在 MapEntity 中设置 cellMap 的值。

//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?

我想知道是否有解决方案可以调用(强制)CellEntity 类中的方法以初始化 cellMap 中的每个 CellEntity?

Well, if your cellMapwas 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被声明为 final,除了可能使用反射(我认为你不会很喜欢它)之外,你无法通过方法(访问器)设置它。

回答by Hans

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

回答by user3437460

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?

我想知道是否有解决方案可以调用(强制)CellEntity 类中的方法以初始化 cellMap 中的每个 CellEntity?

It is actually possible, but the array has to be created first.

这实际上是可能的,但必须先创建数组。

First variation

第一个变体

Create array in constructor first. We cannot change the referenceof cellMap after creation, but we still can assign the values within it:

首先在构造函数中创建数组。我们不能在创建后更改cellMap的引用,但我们仍然可以在其中分配值:

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