Is it possible in Java to find the number of dimensions of an array with an \'a priori\' unknown number of dimensions? That is, if one doesn\'t know the number of dimensions
What I can suggest you is to manage this issue by using an array with only one dimension. Suppose to have N dimensions of sizes:
Then you can define the size of your array as:
int size = S_0*S_1*...*S_N-1;
Then you will initialize it, for instance for an array of integers:
int[] array = new int[size];
Then to refer to an item of the array (to read or write it), you have N indexes (because of N dimensions):
What you need now is a way to transform this N indexes, in one single value that can be used as an index in the one-dimensional array, the generic formula is:
Basically this formula 'skips' the correct number of previous dimensions and points to the right location. This formula can be simply implemented in methods like
int read(int[] array, int[] indexes);
void write(int[] array, int[] indexes, int values);
in order to keep the code readable. You may also implement this technique using a generic type, so the methods might work for any type.