简体   繁体   English

Java中二维数组的最大大小

[英]Maximum size of 2D array in Java

I have to create 1000 heaps, each of which can contain 10^6 nodes.我必须创建 1000 个堆,每个堆可以包含 10^6 个节点。 For easy access of the nodes, for deletion of nodes and for updating keys of nodes, I am planning to create a 2D array of size 10^6 * 1000 in which I'll be storing the references of nodes.为了方便访问节点,删除节点和更新节点的键,我计划创建一个大小为 10^6 * 1000 的二维数组,我将在其中存储节点的引用。 But, is an array of such a big size possible to create in Java?但是,是否可以在 Java 中创建这么大的数组? Is there a better way to access a particular node from the heaps without creating an array?有没有更好的方法可以在不创建数组的情况下从堆访问特定节点? I could go through each node of the heap in order to search for my node, but this process will be of the order n for 1 heap, and if I have to perform a deletion of a particular node from all heaps, the process would take be of the order 1000*n.我可以遍历堆的每个节点来搜索我的节点,但是这个过程将是 n 为 1 个堆的顺序,如果我必须从所有堆中删除一个特定的节点,这个过程将需要数量级为 1000*n。

Java arrays are indexed by int s, so the maximum index is 2^31 - 1 , which is 2147483647 (approx. 2E9), so you should be ok with your 1E9 sized array. Java 数组由int索引,因此最大索引为2^31 - 1 ,即 2147483647(约 2E9),因此您应该可以使用 1E9 大小的数组。 However, you'll need to have enough RAM, don't forget a billion long s will take 8GB of ram.但是,您需要有足够的 RAM,不要忘记 10 亿long s 将占用 8GB 的​​ RAM。

There's a much more detailed discussion in Do Java arrays have a maximum size?Do Java arrays have a max size? 中有更详细的讨论

If you need more memory than max value of int you can use sun.misc.Unsafe.如果您需要比 int 最大值更多的内存,您可以使用 sun.misc.Unsafe。 See here: https://dzone.com/articles/understanding-sunmiscunsafe请参阅此处: https : //dzone.com/articles/understanding-sunmiscunsafe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM