6

I wish to enter one int and another long ex: 1 and 1000000000, and now I wish to create an array of size 1000000000. And then at each index of array, store int val, ex: arr[100000000] = 4.

When I'm trying to do this Netbeans shows me an error in this line:

arr = new long[y+1]` and `arr[j] = 0` 

"Possible Lossy conversion from long to int". Here's my code:-

public static void main(String[] args) throws IOException       
{     
    BufferedReader s = new BufferedReader(new InputStreamReader(System.in));           
    String[] xe = s.readLine().split(" ");          
    int x = Integer.parseInt(xe[0]);        
    long y = Long.parseLong(xe[1]);
    long []arr;    
    arr = new long[y+1];     
    for(long j=0;j<=y;j++)     
    arr[j] = 4;     
} 
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
ranaarjun
  • 245
  • 3
  • 5
  • 12
  • one billion can be stored as an `int` as the limit is ~2.1 billion. Note: one billion `long` values uses 8 GB of memory. Perhaps a `Map` will work better if you only have a few values to set. – Peter Lawrey Feb 17 '14 at 12:46

6 Answers6

15

Array index is an integer in Java and the compiler will advice you. So maximum array size is (aproximately) Integer.MAX_VALUE. For bigger arrays you should use ArrayList.

To go around this dirt&quick

arr = new long[(int)y+1];  
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • so when we typecast from 1000000000 which is long to int... I think in this way we lost some bits.. – ranaarjun Feb 17 '14 at 11:45
  • 1
    An `ArrayList` internally stores an array, so it will not be able to maintain a larger number of elements. – Marco13 Feb 17 '14 at 11:46
  • @ranaarjun Yes, you may experience a runntime error. – PeterMmm Feb 17 '14 at 11:46
  • AFAIK ArrayList is limited by available (JVM) memory. – PeterMmm Feb 17 '14 at 11:47
  • Please note that `Collection.size()` has the return type `int`. I wouldn't say it was safe to assume that any Collection in java can hold more than `Integer.MAX_VALUE` elements. – Bex Feb 17 '14 at 12:12
  • 1
    Perfect! Never new Java arrays can have `Integer.MAX_VALUE` values only. – asgs May 01 '15 at 17:09
  • Correct. The implementation of `ArrayList` in current Java SE versions have a hard limit of `Integer.MAX_VALUE`. However, this does not apply to all collections. Bex's inference from the `Collection.size()` return type is not correct. The `Collection` javadoc states this: *"If this collection contains more than `Integer.MAX_VALUE` elements, returns `Integer.MAX_VALUE`"*. – Stephen C Oct 31 '21 at 02:14
  • It is also worth noting that the `ArrayList` javadoc does not *specify* a limit ... so it is conceivable that a future implementation of `ArrayList` (or a future subclass of `ArrayList`) could support larger lists. – Stephen C Oct 31 '21 at 02:18
2

The size of an array can only be an int. That is you, can not create arrays that are larger than Integer.MAX_VALUE (2147483647), probably a few elements less (depending on the VM). You can cast your value to int

arr = new long[(int)(y+1)];

but this will produce invalid results when the value of y is actually larger than the maximum allowed size.

Marco13
  • 53,703
  • 9
  • 80
  • 159
  • This is not possible without own data structures or dedicated libraries, e.g. http://code.google.com/p/vanilla-java/wiki/HugeCollections (this is one of the first websearch results - not a recommendation, only a hint) – Marco13 Feb 17 '14 at 13:21
1

You need to convert/cast y and j to int. Also your incrementing var shouldn't be long when you're just adding 1.

Leo Pflug
  • 544
  • 3
  • 15
1
  1. You cannot create an array with more than 2^31-1 entries, so you should use an int value when you do so (the compiler is simply warning you that the size will be truncated from long to int). 1000000000 is small enough to fit into int, so you basically have no reason to use long y in order to store this value in the first place.

  2. According to your description, the array itself is designated to store int values, so it doesn't need to be an array of long values.

In short, you can and should change every long in your code to int.

barak manos
  • 29,648
  • 10
  • 62
  • 114
1

I think you have some misconception about typecasting here. In Java down casting is allowed as already mentioned you can down cast it to integer

arr = new long[(int)(y+1)];

But here the main problem is that array allocation should takes integer value so that the same number of homogeneous space can be allocated. If you want more space than an integer range then you should use ArrayList which is dynamically grow-able array because if we give that much liability to a user to declare a large amount array memory then our system may run out of the memory as array is a static memory allocation data structure. The same stands true for

arr[j]=4;

It should be:

arr[(int)j]=4;
Anwar
  • 1,755
  • 1
  • 21
  • 32
AjayLohani
  • 872
  • 1
  • 6
  • 26
0

In static array we cannot use long datatype values for assigning index position. 1.Either convert long to int in for/while/do while loop. 2. Typecast arr[(int)i] to remove error