title: How can I deep copy 3D array of Objects with for loops in Java?
body: <p>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.</p>
  <p>When I create the first two objects (<code>new BufferedImage [ temp1.length ][ ][ ]</code> and <code>new BufferedImage [ temp1[ i ].length ][ ]</code>) arrays to iterate through them, the storage ID's are completely different than the Array ID's that I copied from..</p>
  <p>So far so good...</p>
  <p>(checked it using Debug mode)</p>
  <p>The issue is that after my final new array object loop I do not know how to copy the collected int value <em><strong>k</strong></em>.</p>
  <p>If I set <strong>temp 2</strong> [ <strong>i</strong> ][ <strong>j</strong> ][ <strong>k</strong> ]= <strong>temp1</strong>[ <strong>i</strong> ][ <strong>j</strong> ][ <strong>k</strong> ].. 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 <em><strong>k</strong></em>..</p>
  <p>When I run the code, many locations are set as a new object... but this issue causes multiple <em><strong>null</strong></em> values in my storage locations in run time.</p>
  <p>Tried using the <em><strong>new</strong></em> keyword again, but it's not accepted because I've already have the number of arrays for the 3D arrays.</p>
  <p>So.. how to properly write the last step of this code..</p>
  <pre><code>private BufferedImage [][][] mix(BufferedImage [][][] temp1) {
          
      BufferedImage [][][] temp2 = new BufferedImage [temp1.length][][];
          for(int i=0 ; i &lt;temp1.length;i++) {
              
              
              temp2[i] = new BufferedImage [temp1[i].length][];
              for(int j=0;j&lt;temp1[i].length;j++) {
                      
                  temp2[i][j] = new BufferedImage[temp1[i][j].length];
                  for(int k=0;k&lt;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;
  }
  </code></pre>