Java Array with More Than 4Gb Elements

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



Leave a reply



Submit