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

4
  • This generates a compile error Type mismatch: cannot convert from long to int, not possible loss of precision. Can you post a SSCCE? Commented Jun 12, 2015 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 for what I think may be happening here. Commented Jun 12, 2015 at 14:31
  • @Bohemian : I have posted the screenshot and also all datatypes here are long Commented Jun 12, 2015 at 14:46
  • @sstan : Please check an example here : hackerearth.com/code-monk-sorting/algorithm/… Commented Jun 12, 2015 at 14:50

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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 ...
Yes, you're right. That is why I suggested using some mapping or lazy loading tecnique to reduce memory consumption
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.

Comments

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.

1 Comment

That will result in an out of memory very quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.