0

I have a method that takes in dimensions for an array in a list of params

private int[] anArray;

public MDArray(int... sizes){
anArray = new int[sizes[0]];

for (int u : anArray){
    System.out.println(u);
    }

If I pass in (12,13) as the params into my function, then the loop prints out 0 12 times. I am unsure about how to access the sub layers of the multi-dimensional array.

My understanding is that in Java there is no real multi-dimensional array, rather an array of arrays.

If I try

for (int[] u: anArray) {
  for (int elem: u) {
  System.out.println(elem);
    }
}

This fails as u is not an array but an int.

I would really appreciate a hand with this.

Ben Irving
  • 331
  • 5
  • 21
  • Look at this answer: http://stackoverflow.com/a/17559021/5450645 – J. Su. Feb 21 '16 at 14:19
  • J.su, I have seen this but I get "int cannot be converted to int[]" as an error in "for (int[] a : flatArray)" – Ben Irving Feb 21 '16 at 14:29
  • @cricket_007 it is instantiated in the constructor depending on the list of dimensions passed. So it could be int[], int[][] or int[][][] so I need to convert both 2-d and 3-d arrays to 1-d – Ben Irving Feb 21 '16 at 14:30
  • @cricket_007 sorry fixed now just typo, was meant to be anArray – Ben Irving Feb 21 '16 at 14:33
  • You are declaring `anArray` as `int[]`, i.e. as a 1-dimensional int-array, but it seems like you want to have arbitrary number of dimensions. – tobias_k Feb 21 '16 at 14:36
  • Okay, as I said before, it must be a `int[][]` to do the loop you are trying to do there. You're error exists because there are no `int[]` *within* a `int[]`. Only `int`. In other words, you don't have a multidimensional array... – OneCricketeer Feb 21 '16 at 14:37
  • yes @tobias_k I am not sure how to handle this. – Ben Irving Feb 21 '16 at 14:38
  • Are you opposed to using an Arraylist? I don't think you can make an arbitrarily defined multidimensional array very easily – OneCricketeer Feb 21 '16 at 14:40

1 Answers1

0

It prints out 12 times 0 because of your declaration at

anArray = new int[sizes[0]];

at this point you get the 0. element of the array sizes which is 12 and initialize an 12x1-dimensional-integerarray. It's the same as

anArray = new int[12];

At first to solve your Problem you need to change anArray from nx1-d-Array to a nx2-d-Array (change anArray[] to anArray[][]).

After that you can initialize anArray with your Parameters in that way:

anArray = new int[size[0]][size[1]];

But at that point you need to be clear about that 'int... size' as parameter means. int... size is an integer-array which depends on the value in the function call. In your case you had MDArray(12,13) as call. which got handed over like int[2] with the values (12,13).

So to be sure to have the correct parameters set in moment of calling your function you can simple check for the parameters like:

if(sizes.length>=2){
anArray = new int[size[0]][size[1]];
}

in the if-clause you make sure that you have at least 2 parameters saved in the array.

Edit: sorry forgot something:

Finally you can access that array with something like

for(int[] e :anArray)
        for(int k:e){
          System.out.println(k);
        }
}
Jodn
  • 314
  • 1
  • 11