How to make a big array in java

后端 未结 5 649
抹茶落季
抹茶落季 2020-12-20 17:44

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 creat

5条回答
  •  囚心锁ツ
    2020-12-20 18:18

    First of all: You really need a good reason to allocate this much memory. As others have said, you may want to rethink the approach.

    A few suggestions: Either limit the amount to allocate to some maximum or store it in a file and seek for the data or allocate on an as-needed basis (lazy allocation). If the data is sparse (few actual booleans, but at very widely spread indexes), you are better off with a map. If it's mainly zeroes, consider storing only the ones :)

    Second: It's theoretically possible to allocate 8 * the maximum array size booleans if you pack the bits. See this discussion for inspiration: Implementing a C style bitfield in Java

提交回复
热议问题