How to Copy a 2 Dimensional Array in Java

How do I copy a 2 Dimensional array in Java?

current=old or old=current makes the two array refer to the same thing, so if you subsequently modify current, old will be modified too. To copy the content of an array to another array, use the for loop

for(int i=0; i<old.length; i++)
for(int j=0; j<old[i].length; j++)
old[i][j]=current[i][j];

PS: For a one-dimensional array, you can avoid creating your own for loop by using Arrays.copyOf

Cloning two-dimensional Arrays in Java

you should iterate this array with two loops. This will helps you:

static double[][] clone(double[][] a) {
double[][] b = new double[a.length][];
for (int i = 0; i < a.length; i++) {
b[i]= new double[a[i].length];
for (int j = 0; j < a[i].length; j++)
b[i][j] = a[i][j];
}
return b;
}

How do I do a deep copy of a 2d array in Java?

Yes, you should iterate over 2D boolean array in order to deep copy it. Also look at java.util.Arrays#copyOf methods if you are on Java 6.

I would suggest the next code for Java 6:

public static boolean[][] deepCopy(boolean[][] original) {
if (original == null) {
return null;
}

final boolean[][] result = new boolean[original.length][];
for (int i = 0; i < original.length; i++) {
result[i] = Arrays.copyOf(original[i], original[i].length);
// For Java versions prior to Java 6 use the next:
// System.arraycopy(original[i], 0, result[i], 0, original[i].length);
}
return result;
}

how to copy a multidimensional array to a single array?

Here is the correct way to use the arraycopy:

int copy[] = new int[arr[r].length];
System.arraycopy(arr[r], 0, copy, 0, copy.length);
return copy;

A shorter way of writing the above:

return Arrays.copyOf(arr[r], arr[r].length);

A third way:

return arr[r].clone();

All three ways will have the same result. As for speed, the first two ways may be a tiny bit faster than the third way.

Copy part of two-dimensional array

Maybe return the array and pass the array as a parameter.

int[][] mymethod(int[][]oldArray, int noOfRows){
int[][] newArr;
if(noOfRows > 0){
//here copy from oldArray to new;
//sorry this is where I can't remember, you will have
//to do something to get column size maybe oldArray[0].length()
// but that may not work if each row has different length.
newArr = new int[noOfRows][];
for(int i=0; i < noOfRows; i++){
for(int j = 0; j < noOfRows; j++){
newArr[i][j] = oldArray[i][j];
}
}
}
return newArr;
}

after this just call the method.
int [][] a = mymethod(dArray,5);
int [][] b = mymethod(a, 2);

How to clone a multidimensional array in java?

When the above code is run, arrayMaster changes as well as arrayChanges, contrary to my intentions.

The line

static private int[][] arrayChanges = arrayMaster;

is the culprit. This line makes arrayChanges and arrayMaster point to the same object, so a change to either one is visible when you access the object from either.

EDIT: What happens whenever you clone one dimension of a multidimensional array

As Eric Lippert explains, an array is conceptually a list of variables. If you just assign another variable to point to the same array a la static private int[][] arrayChanges = arrayMaster;, you haven't changed the set of variables at all. You haven't created any new variables except for arrayChanges, so you haven't gotten more memory from the operating system/JVM, so any change you make to arrayMaster is applied to arrayChanges and vice versa.

Now let's look at a two-dimensional array. In Java, a two-dimensional array is a list of variables that happens to have the property that each one of these variables refers to a one-dimensional array. So, whenever you clone a two-dimensional array, you create a new list of variables, each pointing in the same place that the old variables pointed in. So, you have gained a little in that you can safely write arrayChanges[0] = new int[10] without affecting arrayMaster, but as soon as you start referencing arrayChanges[i][j] you are still referencing the same second-level arrays that arrayMaster references. What you really want in order to deep-copy a two-dimensional array of ints is

public static int[][] deepCopyIntMatrix(int[][] input) {
if (input == null)
return null;
int[][] result = new int[input.length][];
for (int r = 0; r < input.length; r++) {
result[r] = input[r].clone();
}
return result;
}

To those who may look at this answer in the future: yes, it is better replace int with T here and make the method generic, but for this purpose a more concrete deep copy method is simpler to explain well.

How to clone a two-dimensional array of float entries to another two-dimensional array of similar type and size?

1 - Change method parameter from int to float.

2 - change return type from int to float.

3 - while assign float value in array use f after every number to tell compiler that its a float number.

public static float[][] clone(float[][] a) throws Exception {
float b[][] = new float[a.length][a[0].length];

for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
b[i][j] = a[i][j];
}
}
return b;
}

float[][] a = new float[][] { { 1.513f, 2.321f, 3.421f }, { 4.213f, 5.432f, 6.123f },
{ 7.214f, 8.213f, 9.213f } };

try {
float b[][] = clone(a);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
} catch (Exception e) {
System.out.println("Error!!!");
}


Related Topics



Leave a reply



Submit