Making a very large Java array

后端 未结 15 2182
鱼传尺愫
鱼传尺愫 2020-12-03 08:57

I\'m trying to find a counterexample to the Pólya Conjecture which will be somewhere in the 900 millions. I\'m using a very efficient algorithm that doesn\'t even require an

15条回答
  •  生来不讨喜
    2020-12-03 09:09

    Java arrays are indexed by int, so an array can't get larger than 2^31 (there are no unsigned ints). So, the maximum size of an array is 2147483648, which consumes (for a plain int[]) 8589934592 bytes (= 8GB).

    Thus, the int-index is usually not a limitation, since you would run out of memory anyway.

    In your algorithm, you should use a List (or a Map) as your data structure instead, and choose an implementation of List (or Map) that can grow beyond 2^31. This can get tricky, since the "usual" implementation ArrayList (and HashMap) uses arrays internally. You will have to implement a custom data structure; e.g. by using a 2-level array (a list/array). When you are at it, you can also try to pack the bits more tightly.

提交回复
热议问题