Java:当将二维自定义类数组传递给构造函数时,我在数组中丢失了值

Ess*_*Dee 0 java arrays constructor multidimensional-array libgdx

我知道标题没有意义,但我想不出如何更好地说话,请耐心等待.

我正在使用java与LibGDX,我正在尝试从保存的像素图加载数据,以在屏幕上呈现地图.在像素图中,每个像素代表六边形的颜色(地图是六边形网格).

问题是:我将数据(特别是颜色)从像素图加载到我称为HexInfo的类的数组中.但是,当我将此数组传递给另一个类以便在我的屏幕上绘制它时,数组中的每个HexInfo项目的颜色都为黑色.以下是一切设置的方法

hexInfo = new HexInfo[cols][rows];

for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    Color.rgba8888ToColor(color, savedScreenData.getPixel(i, j));

                    hexInfo[i][j] = new HexInfo(i,j);
                    hexInfo[i][j].setColour(color);
                    hexInfo[i][j].setIsVisible(true);
                    hexInfo[i][j].setIsOccupied(true);

                    //It is definitely set because this gives the correct colours
                    System.out.println(hexInfo[i][j].getColour());
                }
            }
mapScreen = new MapScreen(hexInfo);
Run Code Online (Sandbox Code Playgroud)

这里^,屏幕上打印的getColour是正确的.

然后,在MapScreen类中,我为每个hexInfo使用for循环来获取getColour:

public MapScreen(HexInfo[][] hexInfo)
{
cols = hexInfo.length;
    rows = hexInfo[0].length;

    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
            if (hexInfo[i][j].isOccupied())
            System.out.println(hexInfo[i][j].getColour());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这里每种颜色都是黑色.

HexInfo类如下:

public class HexInfo {

private Color colour;
private String owner;
private boolean isOccupied;
private boolean isVisible;

public Color getColour() {
    return colour;
}

public String getOwner() {
    return owner;
}

public boolean isOccupied() {
    return isOccupied;
}

public boolean isVisible() {
    return isVisible;
}

public void setIsOccupied(boolean isOccupied) {
    this.isOccupied = isOccupied;
}

public void setColour(Color colour)
{
    this.colour = colour;
    System.out.println("SETTING COLOR IN HEXINFO AS: " + colour); //This also gives correct colour in sysout
}

public void updateOwner(Color color, String owner)
{

    System.out.println("setting colour in hexinfo as " + color);

    this.colour = color;
    this.owner = owner;

    isOccupied = true;
}

public void setIsVisible(boolean isVisible) {
    this.isVisible = isVisible;
}

public HexInfo(int x, int y)
{
    mapCoords = new Vector2(x,y);
    colour = new Color(Color.BLACK);
    isOccupied = false;
}
}
Run Code Online (Sandbox Code Playgroud)

我检查了一些东西,但没看到问题出在哪里.如果它与我的其余代码一起使用,请告诉我您需要的其他信息(有很多,所以我显然没有包含所有这些信息).

Tho*_*mas 5

它与将数组传递给构造函数无关.

HexInfocolor当你打电话时没有制作副本setColour(...),它只是设置一个参考.因此,数组的所有成员最终都将指向同一个color实例,因此它们都将具有最后一个像素的颜色.

更改嵌套for循环的最内部部分应该修复它:

hexInfo[i][j] = new HexInfo(i,j);
hexInfo[i][j].setIsVisible(true);
hexInfo[i][j].setIsOccupied(true);

Color.rgba8888ToColor(hexInfo[i][j].getColour(), savedScreenData.getPixel(i, j));

//It is now *really* definitely set because this gives the correct colours
System.out.println(hexInfo[i][j].getColour());
Run Code Online (Sandbox Code Playgroud)

  • @ tgm1024我知道.但是他/他用相同的`color`对象调用数组中每个成员的`setColour(...)`方法. (2认同)