Do Java arrays have a maximum size?

前端 未结 9 2112
礼貌的吻别
礼貌的吻别 2020-11-21 11:51

Is there a limit to the number of elements a Java array can contain? If so, what is it?

9条回答
  •  离开以前
    2020-11-21 12:02

    Going by this article http://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays:

    Java has been criticized for not supporting arrays of more than 231−1 (about 2.1 billion) elements. This is a limitation of the language; the Java Language Specification, Section 10.4, states that:

    Arrays must be indexed by int values... An attempt to access an array component with a long index value results in a compile-time error.

    Supporting large arrays would also require changes to the JVM. This limitation manifests itself in areas such as collections being limited to 2 billion elements and the inability to memory map files larger than 2 GiB. Java also lacks true multidimensional arrays (contiguously allocated single blocks of memory accessed by a single indirection), which limits performance for scientific and technical computing.

提交回复
热议问题