I'm trying to construct a method able to fill an n-dimensional array at all positions, for example: I have a 2-dimensional array, where that array is 2x2 with only 2 dimensions on a Cartesian plane, for example.
I would like the same method to populate a 3-dimensional array yet 2x2, however, I would not like to place it in the code in the case of two-dimensional loop inside one another and when it is three-dimensional 3 also a loop within the other. I need to use the same multidimensional code, for example: 8 dimensions with a 2x2 array.
My 2D example:
int matrix2d [] [] = new int [2] [2];
for (int i = 0; i <matrix2d.length; i ++) {
for (int j = 0; j <matrix2d [i] .length; j ++) {
System.out.println ("(" + i + "," + j + ")");
}
}
Result: (0.0) (0.1) (1.0) (1.1)
My 3D example:
int matrix3d [] [] [] = new int [2] [2] [2];
for (int i = 0; i <matrix3d.length; i ++) {
for (int j = 0; j <matrix3d [i] .length; j ++) {
for (int l = 0; l <matrix3d [i] [j] .length; l ++) {
System.out.println ("(" + i + "," + j + "," + l + "));
}
}
}
Result: (1.0.0) (1.0.1) (0.1.1) (1.0.1) (1.0.1) (1.1.1)
I would like to have a generic loop method, not needing to include a loop every time I increase the dimension.