Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access multidimensional array programmatically in Java?

Suppose we have multidimensional array, and the number of dimensions is known only at runtime. And suppose we have an integer number of indices.

How to apply indices to array so to access array's element?

UPDATE

Suppose:

int [] indices = new int { 2, 7, 3, ... , 4}; // indices of some element
int X = indices.length; // number of dimensions
Object array = .... // multidimensional array with number of dimensions X

...

I want to fetch the element addressed by indices indices from array.

UPDATE 2

I wrote following code based on recursion:

package tests;

import java.util.Arrays;

public class Try_Multidimensional {

    private static int element;

    public static int[] tail(int[] indices) {
        return Arrays.copyOfRange(indices, 1, indices.length);
    }


    public static Object[] createArray(int ... sizes) {

        Object[] ans = new Object[sizes[0]];

        if( sizes.length == 1 ) {
            for(int i=0; i<ans.length; ++i ) {
                ans[i] = element++;
            }
        }

        else {
            for(int i=0; i<ans.length; ++i) {
                ans[i] = createArray(tail(sizes)); 
            }
        }

        return ans;

    }

    public static Object accessElement(Object object, int ... indices) {

        if( object instanceof Object[] ) {

            Object[] array = (Object[]) object;

            return accessElement(array[indices[0]], tail(indices));

        }

        else {
            return object;
        }

    }

    public static void main(String[] args) {

        element = 0;
        Object array = createArray(4, 5, 12, 7);

        System.out.println(accessElement(array, 0, 0, 0, 0));
        System.out.println(accessElement(array, 0, 0, 0, 1));
        System.out.println(accessElement(array, 1, 0, 10, 0));
        try {
            System.out.println(accessElement(array, 0, 5, 0, 1));
        }
        catch(Exception e) {
            System.out.println(e.toString());
        }

    System.out.println(4*5*12*7-1);
    System.out.println(accessElement(array, 3, 4, 11, 6));

    }

}

The questions are:

1) are there any reliable ready-made methods from JDK and/or famous libraries for this?

2) I was using Object. can it be avoided? can I create/access variable dimensionality array of built-in or specific type? how large is a payoff due to using Object?

like image 525
Suzan Cioc Avatar asked Jun 27 '14 09:06

Suzan Cioc


1 Answers

int index(Object arrayToIndex, int... indices) {
    for (int i = 0; i < indices.length - 1; i++) {
        arrayToIndex = ((Object[]) arrayToIndex)[indices[i]];
    }
    return ((int[]) arrayToIndex)[indices[indices.length-1]];
}

Loop through the dimensions and index each dimension, one at a time. The casts and the special case for the last dimension are going to be annoying, so I recommend wrapping this in some sort of n-dimensional array class. (It looks like some options already exist.)

like image 136
user2357112 supports Monica Avatar answered Oct 15 '22 01:10

user2357112 supports Monica