2

I know its a silly question but I want to know that Is it possible to find the array dimension? when we have the following code

public class ArrayDimentionCheck {

  public static void main(String[] args) {
    Integer[][] arr = { { 1, 2 }, { 2, 3 } };
    printDimension(arr);
    Integer[][] arr1 = { { 1, 2, 4 }, { 2, 3, 6 } };
    printDimension(arr1);
  }

  private static void printDimension(Object arr) {
    System.out.println("given object dimension:");
  }
}

Can we write some generic code in printDimension method to identify the array dimension??

Rakesh Chouhan
  • 1,196
  • 12
  • 28
  • possible duplicate of [Array's length property](http://stackoverflow.com/questions/9297899/arrays-length-property) – hivert Apr 02 '14 at 07:17
  • It is not a duplicate question, what we do when we have nxn dimensional array ?? – Rakesh Chouhan Apr 02 '14 at 07:21
  • 1
    @Rakesh Yes, what I mean is: do you want to know the number of elements in the array, or the dimension? `int[][][]` would be dimension `3`, `int[][]` would be dimension `2`. In your code, both arrays have a dimension of `2`, but with different lengths in each "row" – Andreas Fester Apr 02 '14 at 07:24

5 Answers5

3

but I want result in following format for arr it should be 2x2 and arr1 3x2

You should know, that in java you can do this:

Integer[][] arr = { { 1, 2 }, { 1, 3, 4, 5 } };

So the code below doesn't work for jagged arrays.

Also it doesn't work for 0-length arrays such as:

new boolean[11][0][4]

Here the code is:

public static void printDimension(Object array) {
    if (array == null) {
        System.out.println("Object is null!");
        return;
    }

    if (!array.getClass().isArray()) {
        System.out.println("Object is not array!");
        return;
    }

    String ans = "";
    Object cur = array;
    while (cur != null && cur.getClass().isArray()) {
        ans += "x" + Array.getLength(cur);

        if (!cur.getClass().getComponentType().isPrimitive()) {
            cur = ((Object[]) cur)[0];
        } else {
            break;
        }
    }

    System.out.println(ans.substring(1));
}

Example of usage:

printDimension(new boolean[1][11]);
printDimension(new boolean[1][11][4]);
printDimension(new Integer[14][88]);

Output:

1x11
1x11x4
14x88
Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
2

2-D array is a array of arrays. So each array can have variable length (which represents columns in case of 2-D array). You can get length of individual arrays using

args[0].length;
args[1].length;  

and so on

and length of whole 2-D array using

args.length  

which is number of rows in 2-D array.

Related question: Multi-Dimension length array reflection java

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
1

you can use Array.lenght to know the lenght .. and by using this you can find the dimension . for example

    int xx = arr.length;  // will return the number of row
    int yy = arr[0].length;  // will return the number of column

N.B. assuming that every row has same number of columns

stinepike
  • 54,068
  • 14
  • 92
  • 112
1

I think you are asking obj is single dimension or double dimension or ... of array type.

private static void printDimension(Object arr) {
    final String className = arr.getClass().toString();
    Pattern pattern = Pattern.compile("([\\[])");
    Matcher matcher = pattern.matcher(className);
    int count = 0;
    while (matcher.find()) count++;
    int length = count>1?((Object[][])arr)[0].length:0;
    System.out.println("given object dimension:"+count+"x"+length);
}

...

Integer[][][] arr = new Integer[3][2][2];
printDimension(arr);
Integer[] arr1 = {};
printDimension(arr1);
printDimension(new Object());

result :

given object dimension:3x2
given object dimension:1x0
given object dimension:0x0
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

You can find the dimensions of an array using recursion and reflection.

Given the method dimensions:

private int dimensions(Class arrayType)
{
  return arrayType.isArray() ? 1 + dimensions(arrayType.getComponentType()) : 0
}

When calling the dimensions method like this:

int[] oneDimensionalArray = new int[0];
int[][] twoDimensionalArray = new int[0][0];
int[][][] threeDimensionalArray = new int[0][0][0];

System.out.println(dimensions(oneDimensionalArray.getClass()));
System.out.println(dimensions(twoDimensionalArray.getClass()));
System.out.println(dimensions(threeDimensionalArray.getClass()));

Will print:

1
2
3
Nick Holt
  • 33,455
  • 4
  • 52
  • 58