Question:
Java Merging multidimensional arrays?
boy49er2000
2012-11-27 20:41:42 UTC
will System.arraycopy merge two multidimensional array together? or will i have to write a code for this?
let say we have
x1[][] = {{1,2,3},{1,2,3}};
x2[][] = {{4,5,6},{4,5,6}};

then i want a new array that merges the two arrays to look like this:
x[][] = {{1,2,3}, {1,2,3}, {4,5,6}, {4,5,6}}

help please! thank you!!
Four answers:
?
2012-11-28 02:01:29 UTC
well x[][] = {{1,2,3}, {1,2,3}, {4,5,6}, {4,5,6}} makes no sense since it is 4D array from the way you represented its values.



either it can be {{1,2,3,1,2,3}, {4,5,6,4,5,6}}

or {{1,2,3,4,5,6}, {1,2,3,4,5,6}}
?
2016-11-10 14:00:58 UTC
Java Merge Arrays
Voice of Insanity
2012-11-27 20:47:33 UTC
I would do it like this



int[][] x1 = {{1,2,3},{1,2,3}};

int[][] x2 = {{4,5,6},{4,5,6}};

int[][] x = new int[x1.length+x2.length][];

int ix = 0;

for(int i = 0; i < x1.length; i++) x[ix++] = x1[i].clone();

for(int i = 0; i < x2.length; i++) x[ix++] = x2[i].clone();



The clone method creates a copy of an array. A two dimensional array is an array of one dimensional arrays. If I copy it I must make sure to also copy the arrays contained inside. Otherwise a change to one of the two arrays will automatically change both arrays.

e.g.

int[][] x = {{1,2,3},{1,2,3}};

int[][] y = x.clone();



x[0][0] = 7;

System.out.println(y[0][0]);

This would print out 7 because y[0] is an array and that array is not a copy of x[0] but it's the very same array. However if I do this

int[][] x = {{1,2,3},{1,2,3}};

int[][] y = x.clone();

for(int i = 0; i < y.length; i++) y[i] = y[i].clone();

x[0][0] = 7;

System.out.println(y[0][0]);

Now it prints out 1 since x and y are now independent of each other.



@husoski: My code already solves the problem by calling clone() on every contained array. So there is no need to go through the array again,
husoski
2012-11-27 20:51:25 UTC
That should work, but remember that Java multidimensional arrays are arrays of arrays. So, the rows of the x[][] array will be the very same rows as x1[][] and x2[][] after they are copied. So x2[0][1] = 42; will also set x[2][1] to 42 because x2[0] and x[2] refer to the same object.



If you want a fully independent copy, you will have to make your own "deep copy" code. You might find the java.util.Arrays class useful. It has static methods for creating array copies: copyOf() and copyOfRange().



See: http://docs.oracle.com/javase/7/docs/api/index.html?java/util/Arrays.html



So, after you are done with the high level copy, you can loop through x[][] to replace the rows with copies, using something like:



for (int i=0; i
.... x[i] = Arrays.copyOf(x[i], x[i].length);

}



Actually, since copyOf() will extend (or truncate) the array, you can use it to produce the original x[][] array with the x1[][] rows already copied. Then use arrayCopy to fill in the x2 rows:



int[][] x = Arrays.copyOf(x1, x1.length + x2.length);

System.arrayCopy(x2, 0, x, x1.length, x2.length);

// then loop through x[][] to make copies of the rows, if needed.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...