-1

I was reading this post on Stack overflow: copy a 2d array in java and I am a little confused on how the clone method works here...

public int[][] CopyMap(int[][] Map)
{
    int [][] copy = new int[Map.length][];
    for(int i = 0; i < Map.length; i++)
        copy[i] = Map[i].clone();
    return copy; 
}

I know how to copy using enhanced for loops but I would like to fully understand this way.

1) Why do we put the Map.length in the first set of square brackets but not Map[0].length in the second set of square brackets for int[][] copy = new int[Map.length][];? Don't we have to initialize the length of the columns as well? I'm guessing that we can't clone a 2D array but we can clone a row or column at a time.

2) By cloning the columns one column at a time and putting it into our 2D array it sets the length of the columns for us?

3) Could we reverse this code by doing this

public int[][] CopyMap(int[][] Map)
{
    int [][] copy = new int[][Map[0].length];
    for(int i = 0; i < Map[0].length; i++)
        copy[i] = Map[i].clone();
    return copy; 
}

4) Also copy[i] ? This is a 2D array, so shouldn't it be copy[i][] ? Or something like that.

Community
  • 1
  • 1
Adam
  • 2,070
  • 1
  • 14
  • 18

1 Answers1

1

In Java a 2D array is essentially an array of arrays (possibly with different lengths). It is important to remember this. For example, this is OK:

 int[][] ar = new int[2][];
 ar[0] = new int[8]; // ar[0][0..7]
 ar[1] = new int[4]; // ar[1][0..3]

The syntax new int[8][10] can be used as a convenience to create 8 separate arrays of 10 elements each.

If you are familiar with C: an int[][] in Java is more similar to an int** in C.

Note: Map is a terrible name for a variable in Java; variable names generally start with lowercase letters and there is also a very common base container interface of the same name.

1) Why do we put the Map.length in the first set of square brackets but not Map[0].length in the second set of square brackets for int [][] copy = new int[Map.length][]; ?

Because we are starting with an array of Map.length int[]'s, and then cloning those int[]s one at a time.

Don't we have to initialize the length of the columns as well?

No, because when we go through each int[] in Map, we just use clone() to copy it: copy[i] = Map[i].clone().

By cloning the columns one column at a time and putting it into our 2D array it sets the length of the columns for us?

A "column" is just a concept you made up that is only relevant to tabular data (column-major tabular data in your specific context). Anyways, "setting the length" isn't exactly accurate because it implied that something whose length is being set existed in the first place; but when you do int x[][] = new x[5][], x[0] is null until you assign it to something. By cloning the int[]s one at a time, we're just... cloning them one at a time. So, yes, each clone will be the same size as the original.

3) Could we reverse this code by doing this

public int[][] CopyMap(int[][] Map)
{
   int [][] copy = new int[][Map[0].length];
   for(int i = 0; i < Map[0].length; i++)
       copy[i] = Map[i].clone();
   return copy;
}

No; and hopefully the reason why is clear now that you know that an int[][] is an array of arrays. The expression new int[][size] doesn't make much sense; it says that we want each int[] in the array to have a given size, but it doesn't say how many int[]s are in the array. It's wrong for the same reason that int[] x = new int[] is wrong.

4) Also copy[i] ? This is a 2D array, so shouldn't it be copy[i][] ? Or something like that.

No, it should be copy[i]. I'll leave figuring out the reasons as an exercise.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Also take a look at http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm which explains it well. – Jason C Mar 06 '14 at 04:21