0

I just began studying java. One of the tasks was to create a function to create a reverse 2-dimensional array, for example, if the input array was {1,2}{3,4}, the new array must be {4,3}{2,1}. The code below is the method I created. The problem is that the old array d is affected by the loop along with c, so, in this case, it copies half of the values into c, but also replaces the last half of d, so the second half of c is just a mirrored first half, basically, in the example case both c and d will be like this in the end: {4,3}{3,4}. I checked c==d and c.equals(d) after cloning, both show false.

Also, I tried using Arrays.copy, the result was the same. Plus I want the method to work on the arrays that can have their sub-arrays with different lengths, for example, {1,2}{3}{4,5}, and I don't know if it'll work on such arrays.

static int[][] reversematrix(int[][] d) {
  int[][] c = d.clone();
    for (int i = d.length-1, x = 0; i >= 0; x++, i--) {
       for (int j = d[i].length-1, y = 0; j>=0; y++, j--) {
          c[x][y] = d[i][j];
       }
    }
  return c;

Can you tell me how to make d(imput array) unaffected by the method/loop? I think the problem is in copying, so I'd love to know a proper way of copying a 2D array into a new object, but if it is in something else, please tell me waht it is.

UPD: Thanks to @sascha the solution was found. Here's the code if someone is interested:

static int[][] reversematrix(int[][] d) {
    int[][] c = new int[d.length][];
    for (int i = d.length-1, x = 0; i >= 0; x++, i--) {
        c[x] = new int[d[i].length];
        for (int j = d[i].length-1, y = 0; j>=0; y++, j--) {
        c[x][y] = d[i][j];
   }
}
    return c;
}
Alexandra
  • 9
  • 2
  • 1
    [Related 1](https://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java/13792183), [related 2](https://stackoverflow.com/questions/1686425/copy-a-2d-array-in-java) ... – Oleksandr Pyrohov Jun 09 '19 at 16:57
  • Since you're copying the values yourself anyway, you just need a new 2D array of the right size, so: `int[][] c = int[d.length][d[0].length];` – Andreas Jun 09 '19 at 17:16
  • Andreas, I saw that in another example and tried it after posting. Doesn't support a 2D array with different subarray lengths, which is what I want. – Alexandra Jun 09 '19 at 17:20
  • you can take a look at the answer provided here too. https://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java – Angel Koh Jun 09 '19 at 17:32
  • Okay, I googled it up some more and looked through the given examples. Looks like there is no way to either create a 2D array with different line lengths or deep copy an existing one with the same trait. So I think the way to solve this is to create a 2D arraylist and fill it up with the values of the imput array, then convert it into an array and make the method return it. I hope this is possible, will try it when I'm able to. – Alexandra Jun 09 '19 at 18:02
  • @Alexandra Of course there is. Those are usually called jagged/ragged arrays. Having `int[][] matrix = new int[rows][]` somewhere, you loop over those rows and create the columns you need like: `matrix[0] = new int[3];` and `matrix[1] = new int[4]`. Your problem is the `d.clone()`. More specific: look what clone does on some array if the elements are Objects and not Primitives. And remember: all arrays are Objects in java. So the inner elements of some multi-dimensional array are objects and references are passed. – sascha Jun 09 '19 at 18:13
  • @sascha, thank you, that solved it! – Alexandra Jun 10 '19 at 08:29
  • Funny, I don't see any marks to clock to mark the question as solved... well, maybe something's wrong with the browser. – Alexandra Jun 10 '19 at 08:39

0 Answers0