0

Probably something object-oriented wise I am not understanding here.

I have a 3d array called old_cube. I want to apply modify this array to obtain new_cube.

old_cube:

[[[w, w], [w, w]],
[[o, o], [o, o]],
[[g, g], [g, g]],
[[r, r], [r, r]],
[[b, b], [b, b]],
[[y, y], [y, y]]]

Code:

char[][][] old_cube = state.cube;
char[][][] new_cube = old_cube.clone();

new_cube[0][0][0] = old_cube[0][1][0];
new_cube[0][0][1] = old_cube[0][0][0];
new_cube[0][1][0] = old_cube[0][1][1];
new_cube[0][1][1] = old_cube[0][0][1];

new_cube[1][0][0] = old_cube[2][0][0];
new_cube[1][0][1] = old_cube[2][0][1];

new_cube[2][0][0] = old_cube[3][0][0];
new_cube[2][0][1] = old_cube[3][0][1];

new_cube[3][0][0] = old_cube[4][0][0];
new_cube[3][0][1] = old_cube[4][0][1];

new_cube[4][0][0] = old_cube[1][0][0];
new_cube[4][0][1] = old_cube[1][0][1];

Output:

new_cube

[[[w, w], [w, w]],
[[g, g], [o, o]],
[[r, r], [g, g]],
[[b, b], [r, r]],
[[g, g], [b, b]],
[[y, y], [y, y]]]

Expected:

new_cube

[[[w, w], [w, w]],
[[g, g], [o, o]],
[[r, r], [g, g]],
[[b, b], [r, r]],
[[o, o], [b, b]],
[[y, y], [y, y]]]

The issue is something that I thought clone() would fix, but I must have done something wrong.

Orange Peel
  • 484
  • 5
  • 20
  • I would never clone this as I'd be afraid that I'd likely not create a deep clone. Instead why not create newCube de-novo from the lengths of oldCubes arrays and then fill it with the data desired? – Hovercraft Full Of Eels Jan 18 '15 at 14:13
  • I don't think that Array.clone does a deep cloning. – ITroubs Jan 18 '15 at 14:14
  • Hi @HovercraftFullOfEels. Can you please explain what you mean by a deep clone. – Orange Peel Jan 18 '15 at 14:16
  • Please read [deep cloning](http://www.jusfortechies.com/java/core-java/deepcopy_and_shallowcopy.php) and also [How to clone a multidimensional array in java?](http://stackoverflow.com/questions/9106131/how-to-clone-a-multidimensional-array-in-java) – Hovercraft Full Of Eels Jan 18 '15 at 14:17

0 Answers0