I'm new to Java and I've been wondering lately about memory consumption of huge, but partly empty array like in this case:
- I need array of unknown size- it can be 300k of values, it can be 300m, but to keep in mind it might as well keep only like 50 values,
i'm initialising array with size of
int values[] = new int[Integer.MAX_VALUE];user generates some number of values that has to be stored in array every time given generating method is used
Is there any contraindication of using excessively huge arrays? How the size of array matters in case of performance when, say only 0,1% of that array is used vs 100% array usage?
In other words- when im calling empty array of X int values, will the JVM on initialisation reserve memory for X*(memory used to store 1 int value) even if there isn't any value stored yet?
Thanks, Daniel.
ArrayList?Integer.MAX_VALUEfor storingintvalues? Just no.ArrayList.ArrayListhere, since there is only one reason why OP might create such a large array, but doesn't fill it completely (as far as I see it): he prefers to access the items by their index. So anArrayListis no help here. AMapshould be more suited, withIntegeras the key.int(as an object) as compared to storing into anint[].