Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a big array in java

Tags:

java

I want to create a boolean array of a size which the user is going to put as input.For example - The user might put a big number like 1000000000000 ; so then I have to create a boolean array of the size 1000000000000.The problem I am facing is , I cant store the input as int , as it cant hold such a big number - thus I am unable to create the array .Double is an option .I can store the input number as double , but I don't know how to create the array of the size of the double number .This was the idea -

Scanner scanner = new Scanner(System.in);
int target = scanner.nextInt();
boolean [] array_a=new boolean [(target)];

which wont work if target exceeds the int range.Any help is appreciated.

Update : Thanks guys .So you can only create an array of the size of int's max range (which is 2147483648),right?The memory aspect didn't hit me earlier. Going to take a different approach .

like image 516
Tahniat Ashraf Avatar asked Mar 26 '13 06:03

Tahniat Ashraf


People also ask

How do you make an array bigger?

Once an array has been created, its size cannot be changed. Instead, an array can only be "resized" by creating a new array with the appropriate size and copying the elements from the existing array to the new one.

What is the maximum size of an array in Java?

A Java program can only allocate an array up to a certain size. It generally depends on the JVM that we're using and the platform. Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.

Can we create long array in Java?

Can't i create a long size array in java? No you can't but 2^31-1 elements should be enough (2147483647) How man memory do you have to store more than that amount of array elements? @Scott : you will need an array of arrays.


3 Answers

You can't create an array in Java that has a size greater than the maximum positive int, because array indexes are int. (The same goes for the various List implementations. You may be able to create one with more entries [a LinkedList, for example], but things like get and size start not quite working right, you could only get at later entries via an iterator [assuming things didn't just plain break], which would take a while.)

It seems unlikely that you really need to create an array of boolean with room for more than 2,147,483,647 entries, but if you really do, you'll have to create multiple arrays and choose the right one by taking the modulus of your index (which will need to be a long). (Or use some non-JDK library, if one exists to do that.) That would take something like 4G of RAM. Feasible, but the odds are pretty high that a different approach entirely would be better.

But 1,000,000,000,000 elements? That would require on the order of 1-2 TB of RAM. As NPE says, if you're not running on a supercomputer, you're not going to have that.

like image 154
T.J. Crowder Avatar answered Oct 19 '22 06:10

T.J. Crowder


have to create a boolean array of the size 1000000000000. The problem I am facing is , I cant store the input as int

Your problem isn't that. Your main problem is that you won't have enough memory to allocate a data structure with 1,000,000,000,000 elements (even if you overcame the limitations of int indexing).

You'll need to rethink the algorithm.

like image 26
NPE Avatar answered Oct 19 '22 07:10

NPE


How about using a HashMap and use long keys and boolean values.

There you have several advantages.
1. You can use indexes within the range of long
2. You don't have to worry about the maximum size of item index that is used. As long as it is a long it will work
3. You don't allocate memory for the entire collection up front. Instead you will only use the memory that you need.

like image 41
Rakhitha Avatar answered Oct 19 '22 06:10

Rakhitha