Recursive method or loop to populate or list an n-dimensional array

3

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.

    
asked by anonymous 13.11.2017 / 02:32

2 answers

1

As I do not know the purpose of the code, here is a very basic way to create the points of the array, using recursion:

private static int numLinha;

public static void main (String[] args) throws java.lang.Exception
    {
    int dims = 5;
        int size = 3;
    numLinha = 0;
    getMatrix(dims, size);
}

private static void getMatrix(int dims, int size) {
    int[] arr = new int[dims];
    getMatrixPoint(0, 0, dims, arr, size);
}

private static void getMatrixPoint(int pos, int index, int currDim, int[] arr, int size) {

    //condicao de fim de recursao
    if (currDim == 0) {
        String strPoint = "(";
        String sep = "";
        for (int i = 0; i < arr.length; i++) {
            strPoint = strPoint + sep + arr[i];
            sep = ", ";
        }
        strPoint = strPoint + ")";
        System.out.println(++numLinha + " - " + strPoint);
        return;
    }

    //para cada possivel valor da primeira dimensao
    //calcule os possiveis pontos da (quantidade_de_dimensoes - 1)
    for (int i = 0; i < size; i++) {
        arr[pos] = i;
        getMatrixPoint(pos + 1, i, currDim - 1, arr, size);
        arr[pos] = 0;
    }
}

For an array of size size and number of dims dimensions, size ^ dims points will be generated. See the Ideone

    
14.11.2017 / 19:53
5

It's very difficult to have real problems with more than 3 or 4 dimensions. If you have a problem like this or it is set wrong or you should find a different solution.

Even if you have a problem so it grows exponentially and will need a lot of efficiency. This generalization will create a overhead in execution to control the loop that counts dimensions, which is a greater weight on something that will become very heavy.

There's even a way to do it, but the correct engineering of this is not to do. Better create the individual and specialized case for the dimensions that will actually be used.

It may not be the answer you expected, but it's what you should do. Developing software is not making the smallest code, it is making the best code that solves the problem.

    
13.11.2017 / 11:52