0

I've created an number of BufferedImage Objects and stored them in an 3D array. To shift my objects through the array I need to create a deep copy of the complete 3D array and after.. only after I can change the values of my copied array to return it.

When I create the first two objects (new BufferedImage [ temp1.length ][ ][ ] and new BufferedImage [ temp1[ i ].length ][ ]) arrays to iterate through them, the storage ID's are completely different than the Array ID's that I copied from..

So far so good...

(checked it using Debug mode)

The issue is that after my final new array object loop I do not know how to copy the collected int value k.

If I set temp 2 [ i ][ j ][ k ]= temp1[ i ][ j ][ k ].. the code is accepted but only refers to the stored ID location (shallow copy), it does not create a new one. (In debug mode I can see the ID change to the same ID after instantiating k..

When I run the code, many locations are set as a new object... but this issue causes multiple null values in my storage locations in run time.

Tried using the new keyword again, but it's not accepted because I've already have the number of arrays for the 3D arrays.

So.. how to properly write the last step of this code..

private BufferedImage [][][] mix(BufferedImage [][][] temp1) {
        
    BufferedImage [][][] temp2 = new BufferedImage [temp1.length][][];
        for(int i=0 ; i <temp1.length;i++) {
            
            
            temp2[i] = new BufferedImage [temp1[i].length][];
            for(int j=0;j<temp1[i].length;j++) {
                    
                temp2[i][j] = new BufferedImage[temp1[i][j].length];
                for(int k=0;k<temp1[i][j].length;k++) {
                    

                    // ** before this step..  the storage ID of temp2 = a different one (stil good)
                    
                    temp2[i][j][k] = temp1[i][j][k];
                    
                    // ** after this step..  the storage ID of temp2 = the same as temp 1 (not good)
                }
            }
        }
        return temp2;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You need to create a clone or copy of the `BufferedImage` at `temp1[i][j][k]`, not just copy the reference to it. Here's a previous discussion on [how to clone a BufferedImage](https://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage/26894825) – RaffleBuffle May 07 '20 at 15:32
  • Thank you, SirRaffleBuffle. I'll look in to it for future reference. Note; I started over and re-wrote the same code again .. now I do have another storage ID. I just don't understand what made the difference. – SalvaTorro May 07 '20 at 18:51

0 Answers0