Hello, I became interested in the question: “How much can a large n-dimensional array be created in java?”.

I know that the value of array initialization is limited to integer, namely 2 ^ 31 - 1 (of course, only natural numbers n> 0 are meant). But how much can we declare an array?

That it was clear I will give an example:

long[] a = new long[10]; long[][] a = new long[10][]; long[][]...[] a = new long[10][]...[]; 

I'm interested in curiosity, thank you.

  • I don’t know for sure, but rather everything is as long as the computer’s resources are enough, or limitedly long - Nikita

3 answers 3

The Java Virtual Machine Specification sets a dimension limit of no more than 255:

Represents an array type descriptor, it represents only if it represents 255 or fewer dimensions.

Accordingly, the declaration of an array with a dimension of 256:

 int[][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][] array; 

Causes a compile error:

 error: array type has too many dimensions [][][][][][][][][][][][][][][][] array; ^ 

But this restriction concerns only the explicit declaration of the type of an array of dimensionality 256. At the same time, nothing prevents to declare an array of type Object dimensionality 255 and insert other multidimensional arrays as elements. In this case, the nesting depth is not limited by anything, although reversing by index will require a type conversion.

The same question in English: Maximum number of dimensions in a Java array

  • one
    You have an error on the unit - you say that you declare an array of dimension 255, but in fact 256. And further in the text the same error. - Roman
  • @Roman thanks a lot, corrected - default locale

In the specification of the language itself, there is no limit on the dimension, but in the Java VM it is and is equal to 255.

    In this case, the restriction will be due to the limitations of the Java Virtual Machine - this is a memory limit.

    Experienced I determined that the maximum array size on my machine (java 8) is

     int[] array = new int [Integer.MAX_VALUE - 5]; 

    But nobody forbids us to wrap arrays in a class and get:

     public class SuperLongArray { private static int MASK = Integer.MAX_VALUE - 5; int[] left = new int [Integer.MAX_VALUE - 5]; int[] right = new int [Integer.MAX_VALUE - 5]; public int put(long address, int value) { if(address & MASK > 0) {//если значение указателя больше чем максимальное значение, то мы записывает его в левый массив int pointer = address - MASK; left[address] = value; } else { right[address] = value; } } public int get(long address) { if(address & MASK > 0) { int pointer = address - MASK; return left[address]; } else { return right[address]; } } } 

    Theoretically, you can continue indefinitely, but for the pointer you will have to use either a complex class and try BigDecimal