2

I am trying to solve competitive programming questions using Java. In questions involving the use of arrays, I will have to write my programs such that the array can hold a large number of values(beyond the range of int) and also each array element is also beyond the int range. So I am using long for this. But if I try accessing say

 a[i++] = b[j++];

where i & j & a& b are all of long type, I get the following error:

  error: possible loss of precision

for the index access.

How do I access such elements? I tried typecast of long to int for the index values, and the error goes away, but won't that affect the precision too?

variable declarations are as follows:

long numTest = in.nextLong();

    long [] sizes = new long[numTest];

enter image description here

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • This generates a compile error `Type mismatch: cannot convert from long to int`, not `possible loss of precision`. Can you post a [SSCCE](http://sscce.org)? – Bohemian Jun 12 '15 at 14:29
  • @OP: You do realize that, even if you could achieve what you are asking, you would face some serious memory problems. Have you calculated how much memory such an array would consume? Surely, no competitive programming question would require you to do this. Perhaps you are asking the wrong question? Have a look [here](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) for what I think may be happening here. – sstan Jun 12 '15 at 14:31
  • @Bohemian : I have posted the screenshot and also all datatypes here are long – SoulRayder Jun 12 '15 at 14:46
  • @sstan : Please check an example here : https://www.hackerearth.com/code-monk-sorting/algorithm/chandu-and-his-girlfriend/ – SoulRayder Jun 12 '15 at 14:50

4 Answers4

2

The Java Language Specification says that you can't use longs as array indexes:

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.

So you could expect that tha maximum size would be Integer.MAX_VALUE, but that depends on your JVM which is discussed here

Community
  • 1
  • 1
ottensa
  • 31
  • 2
1

Unfortunately Java can't handle arrays bigger than 2^31 elements, the maximum size for a signed integer. A so big array wouldn't probably fit in memory anyways. Let's consider this case for example:

Array with 2^32 long elements
Size is 2^32 * 8 bytes = 2^35 bytes = 32 GB (!)

In this example the array size is just slightly bigger than the integer maximum value but we already reached 32 gigabytes of used memory.

I think you should find some alternative solution such as memory-mapped files or dinamically loading parts of the data as needed.

I'd also like to link to this existing answer: https://stackoverflow.com/a/10787175

Community
  • 1
  • 1
Iamsomeone
  • 233
  • 2
  • 15
  • To continue the calculation, it seems that an array of bytes indexed with long could reach 128 Zettabytes. Probably more memory than you can afford ... – Guillaume Jun 12 '15 at 15:08
  • Yes, you're right. That is why I suggested using some mapping or lazy loading tecnique to reduce memory consumption – Iamsomeone Jun 12 '15 at 16:37
0

Java arrays must be indexed by int values. That is a rule stated in the language specification. So if you use a long, as an index, the compiler is helping you obey the rules and is implicitly coercing it to an int. That is why you get the message.

To have an array containing a long number of objects you will need a custom implementation of array.

m4ktub
  • 3,061
  • 1
  • 13
  • 17
0

Java allows you to create an array maximum of integer range and hence the indexes should be within the integer range.

To achieve array of long ranges, i suggest you to use the concept of ArrayLet. I.e create 2 dimension arrays where the top level array can help you to cross the integer range, for example see below,

long[][] array = new long[x][Integer.MAX_VALUE];

where x could be any number that defines the multiples of Integer.MAX_VALUE values that your program requires.

Sathish
  • 4,975
  • 3
  • 18
  • 23