[英]Why is there a maximum limit for size of arrays in java?
I got to find from Do Java arrays have a maximum size? 我从Do Java数组中找到了最大的大小? that there is a maximum limit for array size . 数组大小有一个最大限制。
May be with the intention that it might deplete heap space . 可能是因为它可能耗尽堆空间。 Yes, I agree with the point . 是的,我同意这一点。
I cannot understand the following : 我无法理解以下内容:
But why to have this limit for every single array ? 但为什么每个阵列都有这个限制?
what if I have a number of arrays of such large size ? 如果我有这么大的数组?
why not throw an exception when some threshhold of heap space is reached overall(total consumption) , instead of having upperbound for each array ? 为什么不在总体上达到一定的堆空间阈值(总消耗量)时抛出异常,而不是每个数组都有上限?
Note : 注意 :
In Python, they have this limit How Big can a Python Array Get? 在Python中,他们有这个限制Python数组有多大?
In C , there seems like no limit (except the hardware used) The maximum size of an array in C 在C中,似乎没有限制(除了使用的硬件) C中数组的最大大小
Arrays in java are indexed by int values. java中的数组由int值索引。 So even if you have infinte memory, the max no. 所以,即使你有内存,最大的没有。 of elements an array can hold is 2^31-1 (Please correct me if I am wrong with the number... but you get the idea). 阵列可以容纳的元素是2 ^ 31-1(如果我的号码错了,请纠正我......但你明白了)。
However, your memory will limit you on how many such large arrays you can keep in your heap... 但是,你的内存将限制你可以在堆中保留多少这样的大型数组......
The length
of an array is an int
, so you can't allow arrays with more than Integer.MAX_VALUE
elements without breaking length
. 数组的length
是一个int
,因此您不能允许具有超过Integer.MAX_VALUE
元素的数组而不会破坏length
。 Breaking length
would be huge; 打破length
将是巨大的; it would require source changes and recompilation all over the place. 它需要在整个地方进行源代码更改和重新编译。
Given that you already have a length cap mandated by the length
field, there's not a huge difference between a cap of Integer.MAX_VALUE
or something like Integer.MAX_VALUE - 5
. 鉴于您已经有length
字段强制设置的length
上限, Integer.MAX_VALUE
的上限或类似Integer.MAX_VALUE - 5
的上限之间没有太大差异。
Lets say if you have a array 让我们说如果你有一个阵列
String[] array = new String[22L];
Error message in eclipse eclipse中的错误消息
Type mismatch: cannot convert from long to int
Clearly tells that the size of array takes int for length which is limited by ** max int value which is limited by 32-bits** 清楚地告诉我,数组的大小取长度为int,受限于** max int值,受限于32位**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.