0

I know that similar questions have been asked, but after reading their answers, I keep being unable to solbe my problem: I need to implement the Java method clone, which copies all the double entries in a given two-dimensional array a to a newly created two-dimensional array of the same type and size. This method takes the array a as input and returns the new array with the copied values.

IMPORTANT: I am not not allowed to use a library method to clone the array.

Here's what I've done so far: Maybe I didn't understand the requirements but it didn't work:

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

This is the error message I get:

Status: Done
cloneLonger(weblab.UTest) failed: 'java.lang.AssertionError: expected:<3> but was:<2>'
Test score: 2/3
  • What do you mean by "it didn't work"? Do you get any error? Hint: you'll probably need two loops, one inside the other. – sp00m Dec 07 '18 at 11:17
  • 3
    `new double[a.length][a.length]` this is not right. Your solution only considers 1 dimension of `a` for both the sizes of the dimensions of `b` and the copying of the items – Tim Dec 07 '18 at 11:18
  • @TimCastelijns yes, i was thinking that that could be the problem how should I do it instead? –  Dec 07 '18 at 11:19
  • @sp00m edited, my bad –  Dec 07 '18 at 11:20

7 Answers7

2

Something like this should work (with library method):

public class Util{

  // clone two dimensional array
  public static boolean[][] twoDimensionalArrayClone(boolean[][] a) {
    boolean[][] b = new boolean[a.length][];
    for (int i = 0; i < a.length; i++) {
      b[i] = a[i].clone();
    }
    return b;
  }
}
UnP
  • 108
  • 14
1

In this, your code has few mistakes. These corrections were done below. The two-dimension array has 2 lengths. In this case, you didn't consider inside array length.

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

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;
}
Mohsen
  • 4,536
  • 2
  • 27
  • 49
0

You have some logical mistakes:

1. Matrix could be sized M x N where M is the number of rows and N is the number of columns. In your solution you are taking for granted that M is always equal to N.

2. You are iterating trough all the rows and there you do set only one column per row like target[K][K] = source[K][K] -> this will go copy the diagonal only and not the whole matrix.

dbl
  • 1,109
  • 9
  • 17
  • Okay, I understand it, how should I do it then, in order to copy the whoe matrix and in case m and n are different? –  Dec 07 '18 at 11:28
  • 1
    As other answers pointed out: to retrieve `M` -> `source.length`; to retrieve `N` -> `source[0].length; // Returns the length of first item from the source array.`; Use two `for` cycles -> one which goes 0 trough M (`i`) and another one nested, going trough 0 to N (`j`). Set `target[i][j] = source[i][j]` at each iteration. – dbl Dec 07 '18 at 11:34
0

Copy to a temp single row array and then assign it to the out array

static double[][] clone(double[][] a) {
    double[][] b = new double[a.length][];
    for (int i = 0; i < a.length; i++) {
        double[] temp = new double[a[i].length];
        for (int j = 0; j < temp.length; j++) {
            temp[j] = a[i][j];
        }
        b[i] = temp;
    }
    return b;
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

Ever more "advanced" alternatives are:

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][
        //}
        System.arraycopy(a[i], 0, b[i], a[i].length);
    }
    return b;
}

Now there is a utility class Arrays worth knowing:

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

And for primitive arrays the clone method still may be used. Cloning is not very pure, bypassing constructors, and might be dropped from java in some future.

static double[][] clone(double[][] a) {
    double[][] b = new double[a.length][];
    for (int i = 0; i < a.length; i++) {
        b[i] = a[i].clone();
    }
    return b;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
-1

You can't create new array like that. If you are sure that length and width of array is same than only this will work.

class Solution {
    static double[][] clone(double[][] a) {
        boolean[][] b = new boolean[a.length][];
        for (int i = 0; i < a.length; i++) {
            b[i] = new double[a[i].length];
            for (int j = 0; i < a[i].length; j++) {
                  b[i][j] = a[i][j];
            }
        }
        return b;
    }
}
Deepak Kumar
  • 1,246
  • 14
  • 38