大小为 2^32 的 Java 新字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13422515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java new byte array of size 2^32
提问by Eric B
In Java, it won't allow me to use a long for something like this:
在 Java 中,它不允许我使用 long 来做这样的事情:
long size = 0xFFFFFFFF; //2^32-1
byte [] data = new byte[size];
And an int can only go as high as 0x7FFFFFFF (2^31-1). Is it possible to declare a byte array of this size?
而 int 只能达到 0x7FFFFFFF (2^31-1)。是否可以声明这种大小的字节数组?
采纳答案by Yogendra Singh
Answer is NO
as this is the max possible initialization:
答案是NO
因为这是最大可能的初始化:
int size = Integer.MAX_VALUE;
byte [] data = new byte[size];
回答by óscar López
Declare the size
as an int
and try again:
将其声明size
为 an并重int
试:
int size = 0x7FFFFFFF; // 0x7FFFFFFF == Integer.MAX_vALUE == 2^32-1
An array can only be declared to have a positive int
size, not a long
. And notice that the maximum int
positive value (and hence, the maximum possible size for an integer array, assuming there's enough memory available) is 0x7FFFFFFF == Integer.MAX_vALUE
.
数组只能声明为具有正int
大小,而不能声明为long
。并注意最大int
正值(因此,整数数组的最大可能大小,假设有足够的可用内存)是0x7FFFFFFF == Integer.MAX_vALUE
.
回答by irreputable
Integer.MAX_VALUE is the limit for many java concepts. For example String/List can't be longer than that.
Integer.MAX_VALUE 是很多 java 概念的限制。例如,字符串/列表不能长于此。
Back in 1995 when Java was created, 4MB total memory is the norm.
早在 1995 年创建 Java 时,4MB 的总内存是常态。
The same problem exists in most languages. But if we are to design a new language today, we'd certainly use long
.
大多数语言都存在同样的问题。但是,如果我们今天要设计一种新语言,我们肯定会使用long
.
回答by Patricia Shanahan
I agree that you cannot have a byte[] with more than Integer.MAX_VALUE elements. However, you can have an array of arrays.
我同意您不能拥有超过 Integer.MAX_VALUE 元素的 byte[]。但是,您可以拥有一组数组。
I suggest writing a class that has:
我建议编写一个具有以下内容的类:
A constructor that takes a long size and builds a byte[][] big enough to contain that many elements.
Get and set methods that use a long index
A size() method that returns a long.
一个构造函数,它需要很长的大小并构建一个足够大的 byte[][] 以包含那么多元素。
获取和设置使用长索引的方法
返回 long 的 size() 方法。
Of course, this is only practical with a 64 bit JVM and a big enough memory.
当然,这仅适用于 64 位 JVM 和足够大的内存。
I felt even back in the 1990's that the choice of int rather than long for the array index and length types was short-sighted. Sun was selling many machines with tens of gigabytes of memory, and there has been a consistent tendency both for maximum array sizes to approach memory sizes, and for memory sizes to grow.
我什至在 1990 年代就觉得,为数组索引和长度类型选择 int 而不是 long 是短视的。Sun 销售了许多具有数十 GB 内存的机器,并且最大阵列大小趋于接近内存大小,并且内存大小一直在增长。
回答by Bruno Vieira
The problem in your example is that the array create function expects an int as argument and you were passing a long.
您示例中的问题是数组创建函数需要一个 int 作为参数,而您传递的是一个 long。
This compiles:
这编译:
long size = 0xFFFFFFFF; //2^32-1
byte [] data = new byte[(int)size];
But doesn't run. It doesn't run in this particular example of long
because the number is negative, and you simply can't create an array with negative number of elements. But still, it's possible to use a long variable to create an array, but it need to be a positive number and need to be cast to int, like the following:
但不跑。它不会在这个特定示例中运行,long
因为数字是负数,并且您根本无法创建具有负数元素的数组。但是,仍然可以使用 long 变量来创建数组,但它需要是一个正数并且需要转换为 int,如下所示:
long size = 0x00FFFFFF; //
byte [] data = new byte[(int)size];
Which will compile and work.
这将编译和工作。
By the way, changing from size to Integer.MAX_VALUE
won't accomplish nothing if this exceeds your JVM memory limits.
顺便说一句,Integer.MAX_VALUE
如果这超出了您的 JVM 内存限制,从 size 更改为将不会有任何结果。
回答by messivanio
This is not possible. Array size supports only int values.
这不可能。数组大小仅支持 int 值。
You could check the JDK source code for arrays. Only int values are supported.