32

Out of curiosity, how many dimensions of an array can you have in Java?

0

3 Answers 3

49

The Java language does not limit the number of dimensions, but the Java VM spec limits the number of dimensions to 255.

For example, the following code will fail to compile:

class Main {
    public static void main(String[] args) {
        final int[][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][]
                 [][][][][][][][][][][][][][][][] x;
    }
}

with error:

1.java:18: error: array type has too many dimensions
                 [][][][][][][][][][][][][][][][] x;
                                                  ^
1 error

(Ref: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.1 "An array type descriptor is valid only if it represents 255 or fewer dimensions.")

Sign up to request clarification or add additional context in comments.

10 Comments

In other words, if you needed more than 255 (ha!), you would have to make the outer ones just arrays of objects and then cast them to the actual type once you got below 256.
This is interesting - I didn't know that. On one hand, I'm wondering why anyone would ever want to use an array of that dimensionality, and on the other, I'm wondering why they'd limit it like that.
@KennyTM: Well obviously. But if your array has 255+ dimensions, are you really worried about whether it's going to take 8 or 32 bits to store your array's dimensionality?
I for one can't wait to take advantage of java.util.BigArray for n-dimension arrays. "255 dimensions ought to be enough for anybody." indeed.
@Cam it doesn't worth the complexity for something that's extremely rarely used.
|
2

Small experiment shows, that 255 dimensions is maximum. 256 causes compilation error;

The screenshot

Comments

0

Strictly speaking about

 Maximum number of dimensions in a Java array

is only one dimensional array is possible in java. because under the hood java treat multidimensional arrays as array of arrays.

Proof of concept: http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

that's why its possible to have ragged arrays in Java as well!

Comments

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.