How to create a large array in java

I want to create a boolean array of size that the user will put as input. For example: a user can place a large number, for example, 1,000,000,000,000; so I have to create a boolean array of size 1000000000000. The problem I am facing is that I cannot save the input as int, since it cannot hold such a large number - so I cannot create an array. Double is an option. I can save the input number as double, but I do not know how to create an array of the size of a double number. It was an idea -

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

which does not work if the target exceeds the int interval. Any help is appreciated.

Update: Thanks guys. You can create an array of size int max range (which is 2147483648), right? The memory aspect hasn't hit me before. Transition to a different approach.

+7
source share
5 answers

I need to create a logical array of size 1000000000000. The problem that I encountered is that I can not save the input as int

This is not your problem. The main problem is that you do not have enough memory to allocate a data structure with 1,000,000,000,000 elements (even if you have overcome the limitations of int indexing).

You will need to revise the algorithm.

+7
source

You cannot create an array in Java that is larger than the maximum positive int because the indices of the array are int . (The same goes for the various List implementations . You may be able to create one with a large number of entries [a LinkedList , for example], but things like get and size do not work correctly, you can only get later entries through iterator [ assuming things weren't just ordinary), which would take some time.)

It seems unlikely that you really need to create a boolean array with space for more than 2,147,483,647 entries, but if you really do, you will have to create several arrays and choose the right one, taking the module your index (which should be long ). (Or use some library other than the JDK, if one exists). It takes about 4 GB of RAM. Perhaps, but the chances are pretty high that a completely different approach would be better.

But 1,000,000,000,000 elements? This will require about 1-2 TB of RAM. As NPE says, if you don’t work on a supercomputer, you won’t get it.

+11
source

How to use a HashMap and use long keys and booleans.

There you have several advantages.
1. You can use indexes in the long range 2. You do not need to worry about the maximum size of the element index used. While it will be long, it will work 3. You do not allocate memory for the entire collection in front. Instead, you will only use the memory you need.

+1
source

You can create an abstraction, like an array of arrays (you can even change this).

The object [] [] can be logical or any.

 class LargeArray { private final long size; private final int sizeI; private final int sizeJ; private final Object [][] objects; public LargeArray() { sizeI = 3;//Any reasonable value; sizeJ = Integer.MAX_VALUE; objects = new Object [sizeI][sizeJ]; size = sizeI * sizeJ; } public long size() { return size; } public Object get(long index) { int i = index / sizeJ; int j = index % sizeJ; return objects[i][j]; } public void set(long index, Object object) { int i = index / sizeJ; int j = index % sizeJ; objects[i][j] = object; } } 

You can also change the first dimension, say 3. In this case, Object [3] [Integer.MAX_VALUE], you can create (2 ^ 31 -1) * 3 = 2,147,483,647 * 3 = 6442450941 elements, and you need (2 ^ 31 - 1) * 3 * 4 = ~ 23 GB of RAM, which is actually possible !!! :)

+1
source

First of all: you really need a good reason to allocate so much memory. As others have said, you can reconsider the approach.

A few suggestions: either limit the amount to be allocated, or save it in a file, or request data or select as necessary (lazy selection). If the data is sparse (a few actual booleans, but with very common indices), you are better off with a map. If it's basically zeros, consider saving only those :)

Second: theoretically, you can allocate 8 * the maximum size of an array of booleans if you are packing bits. See Discussion for inspiration: Implementing a C-style bitfield in Java

0
source

All Articles