0

Is there a way of changing the number of dimensions an array has, i.e making this

int[][] i = new int[3][3];

but using it like this

getArray(i); //where getArray only accepts one dimensional arrays

?

Alex Dukhan
  • 61
  • 1
  • 8
  • @StephenC yea, though I am confused on the object thing. How would that help (I am new the "Object" object type) – Alex Dukhan May 20 '18 at 01:44
  • Possible duplicate of [Flatten nested arrays in java](https://stackoverflow.com/questions/31851548/flatten-nested-arrays-in-java) – k_ssb May 20 '18 at 01:44
  • @pkpnd - Not a dup. This question is really about accessing, not converting / reshaping the array. (Yes I know the OP says "changing" ... but that isn't what his example is doing ... by my interpretation of what a getter does.) – Stephen C May 20 '18 at 01:46
  • @AlexDukhan Do you want to transform `i` into a one-dimensional array, `int[9]`, so `getArray` operates on `int` elements? Or, do you want `getArray` to operate on the `int[]` elements contained inside `i`? – k_ssb May 20 '18 at 01:46
  • @pkpnd sort of. I would hope that I dont have to loose the second dimension, and as you said, can simply access it as 1D. – Alex Dukhan May 20 '18 at 01:49
  • @AlexDukhan Can you provide a bit more context? Say, are you trying to get an element at index `5`, which would really be `i[1][2]`? – k_ssb May 20 '18 at 01:50
  • @pkpnd No, I want do this sort of thing: Int getArray (int [] array) { do stuff to i}; and then have an: int[][] i = stuff; and then do: getArray(i) – Alex Dukhan May 20 '18 at 01:53
  • 1
    That's exactly my question, _what stuff are you doing to the array_? – k_ssb May 20 '18 at 02:00

2 Answers2

1

You cannot change the number of dimensions in a Java array or array type.

But you can make use of the fact that a Java array is an object ... and subtyping .. and declare a getArray method like this:

 Object getArray(Object[], ...) { .... }

You can call this method on a int[][] instance, but a runtime typecast is needed to cast the result to an int[].

For example:

Object getArray(Object[] array, int i) { return array[i]; }

int[][] big = new int[3][3];
int[] slice = (int[]) getArray(big, 0);

On the other hand, if you are really asking about how to flatten a multi-dimensional array into a 1-D array, the getArray method needs to allocate a new array, fill it from the original and return it.

Note you would be returning a brand new array that is unconnected to the original one. And copying an N x N .... x N array is expensive.

For more details: Flatten nested arrays in java

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Ok, I think I understand, you're just down-casting it. I'll use that in my code. Thank you for all of the help! – Alex Dukhan May 20 '18 at 01:58
0

Java is statically-typed language. This means that you cannot change a variable's type at runtime. But in this particular case you can simply use the following invocation:

getArray(i[2]); // put anything between 0 and (outerArrayLength-1) instead of 2 here
John McClane
  • 3,498
  • 3
  • 12
  • 33