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??

3
  • possible duplicate of Array's length property Commented Apr 2, 2014 at 7:17
  • It is not a duplicate question, what we do when we have nxn dimensional array ?? Commented Apr 2, 2014 at 7: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" Commented Apr 2, 2014 at 7:24

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

2 Comments

@RakeshChouhan, I think in this case you should accept my answer ;)
Yes @Philip this is exactly what I want.
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

2 Comments

what we do when we have nxn dimensional array ??
I have added link to my answer.
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

Comments

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

Comments

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

2 Comments

Thanks @Subhrajyoti but I want result in following format for arr it should be 2x2 and arr1 3x2
args[0].length will help you get that detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.