Populating an array of a custom object modify the fields of another objects into the array

StackOverflow https://stackoverflow.com/questions/22514872

  •  17-06-2023
  •  | 
  •  

Question

i have created a class named MipMapData to keep info about mipmaps, it keeps dimensions, datasize, and the imageformat. From another part of the code i create an array of that MipMapData objects, the problem is that each time i add a new MipMapData object into the array all the mimpamp info objects into the array gets their dimension field modified, and when i populate all the array the result is that all the objects has the same value in their dimension field as the last value added into the array. What i am doing wrong with the dimensions field?

    import java.awt.Dimension;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;


public class MipMapData {
    private Dimension dimension;
    private Long dataSize;
    private Image.Format format;

//Constructor calculates size. Has this arguments to avoid the existence of mipmap incomplete data or with errors in datasize
public MipMapData(Image.Format format, Dimension dimension){
    this.format=format;
    setDimension(dimension);
}



public Dimension getDimension() {
    return dimension;
}
public void setDimension(Dimension dimension) {
    this.dimension = dimension;


    if(format==Format.DXT5||format==Format.DXT3){
        this.dataSize=(long) (dimension.height*dimension.width);
        if(this.dataSize<16)
            this.dataSize=(long) 16;
        return;
    }
    else if(format==Format.DXT1){
        this.dataSize=(long)((dimension.height*dimension.width)/2);
        if(this.dataSize<8)
            this.dataSize=(long) 8;
        return;
    }
    else if(format==Format.ABGR8){
        this.dataSize=(long)(dimension.height*dimension.width*4);
        return;
    }
    else if(format==Format.Luminance8||format==Format.Depth){
        this.dataSize=(long)(dimension.height*dimension.width);
        return;
    }
}




public Long getDataSize() {
    return dataSize;
}


//The datasize of each mipmap is automatically calculated in the constructor
static MipMapData[] createMipMapDataArray(Dimension textureDimensions, int numberOfMipMaps, Image.Format imageFormat){
    int width=textureDimensions.width;
    int height=textureDimensions.height;
    Dimension mipMapDimension= new Dimension();
    MipMapData[] mipMapDataArray= new MipMapData[11];

    if(imageFormat==Format.DXT5|| imageFormat==Format.DXT3){
        for(int i=0;i<=10;i++){
            if((width)<1)
                width=1;
            if((height)<1)
                height=1;
            mipMapDimension.height=height;
            mipMapDimension.width=width;
            mipMapDataArray[i]= new MipMapData(imageFormat,mipMapDimension);
            //actualize dimensions for next mipmap
            width/=2;
            height/=2;
        }
    }
    else if(imageFormat==Format.DXT1){
        for(int i=0;i<=10;i++){
            if((width)<1)
                width=1;
            if((height)<1)
                height=1;
            mipMapDimension.height=height;
            mipMapDimension.width=width;
            mipMapDataArray[i]= new MipMapData(imageFormat,mipMapDimension);
            //actualize dimensions for next mipmap
            width/=2;
            height/=2;

        }
    }
    else if(imageFormat==Format.ABGR8||imageFormat==Format.Luminance8||imageFormat==Format.Depth){
        for(int i=0; i<=10;i++){
            if((width)<1)
                width=1;
            if((height)<1)
                height=1;
            mipMapDimension.height=height;
            mipMapDimension.width=width;
            mipMapDataArray[i]= new MipMapData(imageFormat,mipMapDimension);
            //actualize dimensions for next mipmap
            width/=2;
            height/=2;

        }
    }
    return mipMapDataArray;
}

}

Was it helpful?

Solution 2

You are creating a single Dimension object outside your loop that you are assigning to all your MipMapData objects.

Dimension mipMapDimension= new Dimension();

Remember that in Java objects are passed by reference. So when you pass in mipMapDimension to your MipMapData constructor, it is going to be referenced in all of them.

OTHER TIPS

The reason all fields are getting set to the same when you call createMinMapDataArray() is because you check for certain conditions like

if(imageFormat==Format.DXT5|| imageFormat==Format.DXT3)

You then iterate through the array and initialize the indices with a new MinMap() object that is the same for each index.

The concept of adding to arrays is not really the right way to think about an array. Arrays are used to store information usually of a predetermined size. To add an object to the ith index you would do:

MipMapData[] minMaps = createMipMapDataArray(// pass your parameters here)
minMaps[10] = new MinMap(// pass your parameters)

Of course this does not add the value to your array. It overrides/replaces the value of minMaps[10] with the new value.

To add an undetermined amount of MinMaps to a list, consider using an ArrayList<MinMap>

ArrayList<MinMap> minMapList = new ArrayList<MinMap>();
minMapList.add(new MinMap(// pass your parameters here));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top