Java array with more than 4gb elements
Java array indices are of type int
(4 bytes or 32 bits), so I'm afraid you're limited to 231 − 1 or 2147483647 slots in your array. I'd read the data into another data structure, like a 2D array.
converting 'int' to 'long' or accessing too long array with 'long'
You wouldn't - array indexes are always int
values in Java. It doesn't allow for an array with more than Integer.MAX_VALUE
elements.
The length of an array is represented by the length
field, which is of type int
. Therefore it is impossible to create an array with a length greater than Integer.MAX_VALUE
.
The spec doesn't explicitly call this out, but you can infer it from the types involved.
How to set array dimension to long number
You can't set an array to a size larger than Integer.MAX_VALUE
, the java specs has the following to say:
Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.
An attempt to access an array component with a long index value results in a compile-time error.
Thus you can't create an array with a size larger than the maxvalue of integers. The why I don't know, but my guess is that it has something to do with optimization. So it seems that you have to go with an ArrayList
.
Related Topics
Java List Sorting: How to Keep a List Permantly Sorted Automatically Like Treemap
Java:Read Last N Lines of a Huge File
Explanation of Generic <T Extends Comparable<? Super T>> in Collection.Sort/ Comparable Code
Java String Array: Is There a Size of Method
How to Set Eclipse Console Locale/Language
How to Add Close Button to a Jtabbedpane Tab
How to Clone a Generic List in Java
Programmatically Import Ca Trust Cert into Existing Keystore File Without Using Keytool
Centering Text in a Jtextarea or Jtextpane - Horizontal Text Alignment
Netbeans - Error: Could Not Find or Load Main Class
How to Give System Property to My Test via Gradle and -D
Using Spring Resttemplate in Generic Method with Generic Parameter
Generating 8-Character Only Uuids
Are Thread.Sleep(0) and Thread.Yield() Statements Equivalent
Create a List of Primitive Int
Understanding the etc/Gmt Time Zone