0

For school work I need to write a constructor for a class that contains a 2-dimensional array of integers. The constructor copies a passed in two-dimensional array. Below is the code I have so far. The current issue I have is how to initialize the array when the "column" size of the passed in array is unknow. The issue I think I am having is when creating and initializing the array. The length of the inner and out array is unknown.

 public IntMatrix (int[][] array)
    {_matrix = new int [array.length][array.length-1].length];
    for (int i = 0; i < array.length; i++) {
        for(int j=0; j < array[i].length; j++)
        _matrix[i][j]=array[i][j];
    }
}
user1454994
  • 303
  • 3
  • 5
  • 12
  • Possible duplicate of [How do I copy a 2 Dimensional array in Java?](http://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java) – AdamSkywalker Jan 28 '16 at 17:33
  • There is no such thing as a 2D array. What you have there is an array of arrays. Thinking about it this way helps: you know how many rows the outer array has: array.length. And you know how many elements each inner array has: array[i].length. Create the outer array first, and create each inner array when you know its size: inside the loop. – JB Nizet Jan 28 '16 at 17:36
  • 1
    @AdamSkywalker I saw that and this is not a duplicate. In that case, you linked the array being copied has both "rows" and "columns" as known. In this case I am copying from one that I do not know. – user1454994 Jan 28 '16 at 17:40
  • @JBNizet I have seen it in my school book as multi-dimensional arrays. The question is I do not know how many elements are in the inner array so how do I create the array? – user1454994 Jan 28 '16 at 17:42

2 Answers2

1

As I said in a comment, what you have is an array of arrays:

public IntMatrix(int[][] array) {
    matrix = new int[array.length][];
    for (int i = 0; i < array.length; i++) {
        matrix[i] = new int[array[i].length];
        for(int j=0; j < array[i].length; j++) {
            matrix[i][j] = array[i][j];
        }
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Ok so you are saying I can create a multi dimensional array where the inner array is unknown? I thought I need to specify the sizes of the outer and inner arrays when creating the arrays. – user1454994 Jan 28 '16 at 17:55
  • 1
    Well, no, you don't. As I said: it's simply an array of arrays. The first inner array could be of length 4, the second could be empty, the third of length 342, and the fourth one might be null. This is very different from a 2D array, where every rows has the same length. – JB Nizet Jan 28 '16 at 18:29
  • As it happens to be, the class I need to write is a matrix, which would be a 2D array. But I see it could work in this case as well. – user1454994 Jan 28 '16 at 19:58
0

You can always determine the size of an array via myArray.length, so you can allocate for each row/column as you iterate through.

A thought, however. Is it acceptable to simply store the reference to the array that you're passed ? Will it change outside your class ? If not, then that might be a simple solution if you don't have to recreate the array internally.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440