Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting unknown number of dimensions from a multidimensional array in Java

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 of a multidimensional matrix, how could it be retrieved, if possible?

like image 637
shirowww Avatar asked Apr 22 '14 21:04

shirowww


People also ask

How do you find the total number of elements in a multidimensional array?

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.

How many dimensions can multidimensional arrays have?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

How do you find the dimensions of a 2D array?

To get the length of a 2D Array in Python: Pass the entire array to the len() function to get the number of rows. Pass the first array element to the len() function to get the number of columns. Multiply the number of rows by the number of columns to get the total.

Can we declare 2D array without size in Java?

You can see 2D array offers a lot of flexibility and power e.g. you can declare a 2D array without specifying the second dimension, you can declare a two-dimensional array where each subarray is of a different length, and you can even create a heterogeneous 2D or two-dimensional array in Java.


2 Answers

Does anyone know if it is possible in Java to get 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 of a multidimensional matrix, how could it be retrieved, if possible?

I'm not quiet sure if I understood correctly what you are trying to accomplish. If you just want to know how many elements are there in array, Anubian's answer is correct. But what I understood is that you want to calculate number of dimensions of a given general array.

public class Test {
    public static int getNumberOfDimensions(Class<?> type) {
        if (type.getComponentType() == null) {
            return 0;
        } else {
            return getNumberOfDimensions(type.getComponentType()) + 1;
        }
    }

    public static void main(String[] args) {
        System.out.println(getNumberOfDimensions(int[][][].class)   == 3);
        System.out.println(getNumberOfDimensions(int[][].class)     == 2);
        System.out.println(getNumberOfDimensions(int[][][][].class) == 4);
        System.out.println(getNumberOfDimensions(int.class)         == 0);
    }
}

If that's not what are you looking for, I'd have a hint: there is a difference between a length and dimension.


Update: I think this is completely irrelevant to what we were asked, but Nicola asked me in the comments:

This works perfectly, but what about if the number of dimensions is defined at run-time (for instance the user has to input the desired amount of dimensions)? How you could define and initialize the array?

The solution lies in some light reflection-based hacking:

import java.lang.reflect.Array;

public class Test {
    public static Class<?> getArrayType(Class<?> componentType, int dimensions) throws ClassNotFoundException {
        if (dimensions == 0) {
            return componentType;
        }

        String rawName = componentType.getName();
        switch (rawName) {
            case "byte":    rawName = "B"; break;
            case "char":    rawName = "C"; break;
            case "double":  rawName = "D"; break;
            case "float":   rawName = "F"; break;
            case "int":     rawName = "I"; break;
            case "long":    rawName = "J"; break;
            case "short":   rawName = "S"; break;
            case "boolean": rawName = "Z"; break;
            default:
                rawName = "L" + rawName + ";";
                break;
        }

        for (int i = 0; i < dimensions; i++) {
            rawName = "[" + rawName;
        }

        return Class.forName(rawName);
    }

    public static Object createArray(Class<?> componentType, int dimensions, int length) throws NegativeArraySizeException, ClassNotFoundException {
        if (dimensions == 0) {
            return null;
        }

        Object array = Array.newInstance(getArrayType(componentType, dimensions - 1), length);

        for (int i = 0; i < length; i++) {
            Array.set(array, i, createArray(componentType, dimensions - 1, length));
        }

        return array;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        Object object = createArray(Integer.class, 3, 10);
        System.out.println(object.getClass());
    }
}

The trick is to construct a Class for N-dimensional array using a given component type. We can do that if we know how class names are stored on the lowest level. Rest of the code is just a simple not-interesting recursion.

like image 111
tzima Avatar answered Oct 11 '22 16:10

tzima


Assuming your matrix is called m:

You can get the number of columns by running

m.length;

With Java, multidimensional arrays are actually arrays of arrays. The number of rows is variable. Assuming i is an integer between 0 and m.length-1 you can do:

m[i].length;

To get the number of elements in the row.

like image 26
Anubian Noob Avatar answered Oct 11 '22 15:10

Anubian Noob