0

how to iterate through array/ list in Java. if index range is in long. As array/list only accept integer in position index.

eg

long arr[]=new long[5];
for(long i=0l;i<5l;i++)
     arr[i]=i;  // throw an error as arr[i] only accept integer

here arr[i] will throw an error because i is of type long and array takes input as integer in index location.

Can anyone help me out with this?

2
  • You can't. Array indices are limited from 0 to Integer.MAX_VALUE by design. If you need to access more elements you should think about a different data structure that does not use one array internally. You can use LinkedList (or other Lists) or multiple arrays. But if you really need to manage that much objects in one datastructure this probably is a XY problem and you should think about rethinking your approach. Commented Mar 2, 2018 at 13:27
  • Note that if your long values represent valid integer values you can always cast to int and then use the value as index. Commented Mar 2, 2018 at 13:28

3 Answers 3

2

The size limit1 on arrays is Integer.MAX_VALUE so an array index that is a long makes no sense.

A List can have more than Integer.MAX_VALUE elements, but indexing it will be problematic because List::get takes an int argument.

So, you will struggle to use either arrays or lists (implemented using List) for really large data structures.

The solution ... if you really need one ... would be to implement your own List class with overload or alternative for operations that expose the size and indexing. It would not be trivial.

Another (possibly simpler) approach would be to represent your long[] as a long[][] and map the subscripts.

Finally, if you are using long as a subscript unnecessarily (i.e. the indexes don't need to go beyond Integer.MAX_VALUE), then:

   long arr[] = new long[5];
   for (long i = 0l; i < 5l; i++) {
       arr[(int) i] = i; 
   }

1 - This is the theoretical maximum size. 1) You may get an OutOfMemoryError if you attempt to allocate an array that large. The JVM needs at least length * size(<type>) bytes of free contiguous storage to allocate an array of <type> of length length, where size(<type>) is the size of a primitive type or a reference. 2) In a 32 bit JVM, you are also limited by the address space dimensions. 3) In recent Hotspot JVMs, the largest array that you can allocate is actually Integer.MAX_VALUE - 5 elements: see Do Java arrays have a maximum size?

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

3 Comments

Are you sure that the limit is Integer.MAX_VALUE? Would it be more correct to say that the max size of an array depends on memory od virtual machine? When trying to initialize array with Integer.MAX_VALUE I get OutOfMemoryError.
Arrays are limited by Integer.MAX_VALUE. Of course, your memory is always a factor. It's a large value, such an array needs to allocate space for 2 Mrd objects. Even if the objects are only 10 Byte big (unrealistic small) it would already need 20 GB of memory.
I have addressed this in the footnote. It is actually a bit more complicated than that ...
0

You don't need the index to be long even if your array consists of long's. Change it to

for(int i = 0; ....)

Comments

0

Two solutions:

  1. Narrow the long to an integer by a cast arr[(int)i] = i;
  2. Change the type of i to an integer and let the compiler widen it for you when it is assigned for(int i = 0; i < 5; i++) arr[i] = i;

As you cannot map all long values to a integer the compiler will not automatically narrow a variable. As you can map all integer values to a long the compiler will widen a variable automatically for you.

Comments

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.