[英]How could ArrayDeque has unlimited size with an array backup
I read this "Array deques have no capacity restrictions" from here:我从这里读到这个“Array deques have no capacity restrictions” :
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html#:~:text=Array%20deques%20have%20no%20capacity,Null%20elements%20are%20prohibited . https://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html#:~:text=Array%20deques%20have%20no%20capacity,Null%20elements%20are%20prohibited 。
However, in the source code I found it's using an array (maximum capacity is Integer.MAX_VALUE), and it will throw exception when growing up:但是在源码中我发现它使用的是数组(最大容量为Integer.MAX_VALUE),长大后会抛出异常:
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) {
if (minCapacity < 0)
throw new IllegalStateException("Sorry, deque too big");
return Integer.MAX_VALUE;
}
I'm confused, does ArrayDeque really have unlimited size?我很困惑,ArrayDeque 真的有无限大小吗?
As Thomas already mentioned in his comment, the practical limit is Integer.MAX_VALUE
which equals to 2,147,483,647
~ 68 GB
.正如 Thomas 在他的评论中已经提到的,实际限制是
Integer.MAX_VALUE
等于2,147,483,647
~ 68 GB
。 Thus, the javadoc of ArrayDeque is only correct regarding the theory behind it, but not the actual implementation.因此,ArrayDeque 的 javadoc 仅在其背后的理论方面是正确的,而不是实际实现。
I cannot think of a scenario, which would justify to use ArrayDeque to its maximum capacity.我想不出一个场景可以证明将 ArrayDeque 用于其最大容量是合理的。 IMHO it would only be the symptom of a very bad code design and should be avoided.
恕我直言,这只是代码设计非常糟糕的症状,应该避免。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.