双端队列的 ArrayDeque 类
由于 ArrayDeque 类实现了 Deque 并且它没有任何容量限制。 addFirst()
、addLast()
等异常抛出方法的目的是什么?由于数组没有边界,因此在任何情况下都会添加元素。有人可以解释一下我们可以在 try{}catch{}
块中使用的实现以及 addFirst 可能抛出异常的场景吗?
try{ArrayDeque adObj = new ArrayDeque();
adObj.addFirst("Oracle");//we can keep on adding first. Use to exception handling?
}catch(Exception e){
}
Since ArrayDeque class implements Deque and since it doesn't have any capacity restrictions.
What is the purpose of Exception throwing methods like addFirst()
, addLast()
, etc? It will add the elements in any case since the array has no boundaries. Can someone please explain the with an implementation where we could use within try{}catch{}
block and a scenario where addFirst could throw an exception?
try{ArrayDeque adObj = new ArrayDeque();
adObj.addFirst("Oracle");//we can keep on adding first. Use to exception handling?
}catch(Exception e){
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ArrayDeque 确实存在潜在的容量问题,这意味着它可能会抛出异常。每次扩展时容量都会增加一倍,因此最终无法再增加一倍。该代码的一个实现执行以下操作:
通过如下所示的
addFirst
定义,此方法可以抛出接口文档中描述的至少两个异常。正如其他人提到的,接口上的 JavaDoc 只是给出了可能的异常。它抛出的所有类型都不是检查异常,因此您不必被迫捕获它们。
ArrayDeque
does have potential capacity problems that mean it could throw. It doubles capacity each time it is expanded so eventually it can't double any more. One implementation of the code does the following:With the definition of
addFirst
being as below this method can throw at least two of the exceptions described in the documentation on the interface.As others have mentioned the JavaDoc on the interface just gives possible exceptions. None of the types it throws are checked exceptions so you aren't forced to catch them.
唯一的例外是
ArrayDequeue.addFirst()
被记录为抛出 是NullPointerException
。由于这是一个未经检查的异常,因此您不需要该 catch 块。The only exception that
ArrayDequeue.addFirst()
is documented to throw is aNullPointerException
. Since that is an unchecked exception, you don't need that catch-block.Deque
的某些实现是绑定的(即容量有限),有些则不是。如果达到限制,诸如addFirst
之类的方法会抛出IllegalStateException
。其他方法(例如offerFirst
)返回一个布尔值来指示完全相同的结果。如果您不想处理潜在的异常,或者知道不会有任何异常,只需使用
offerFirst
并忽略结果。Some implementations of
Deque
are bound (i.e. limited in capacity), some are not. Methods such asaddFirst
throw anIllegalStateException
if the limit has been reached. Other methods such asofferFirst
return a boolean value to indicate the very same result.If you don't want to handle a potential exception, or know there won't be any, just use
offerFirst
and ignore the result.Deque
会抛出IllegalStateException
,因为 Java 允许您使用不同的或创建自己的Deque
实现,这可能有大小限制。ArrayDeque
不会抛出这些异常,因此,如果您绝对确定您的代码将使用ArrayDeque
,那么就这样声明它们,它们将不会抛出IllegalStateException
>A
Deque
throwsIllegalStateException
because Java allows you to use a different or create your own implementation ofDeque
, which may have size restrictions.ArrayDeque
does not throw these exceptions, so if you are absolutely certain that your code will useArrayDeque
then declare them as such and they will not throwIllegalStateException