双端队列的 ArrayDeque 类

发布于 2024-10-31 07:31:06 字数 374 浏览 6 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烛影斜 2024-11-07 07:31:06

ArrayDeque 确实存在潜在的容量问题,这意味着它可能会抛出异常。每次扩展时容量都会增加一倍,因此最终无法再增加一倍。该代码的一个实现执行以下操作:

private void doubleCapacity() {
    int n = elements.length;
    int newCapacity = n << 1;
    if (newCapacity < 0)
        throw new IllegalStateException("Sorry, deque too big");
}

通过如下所示的 addFirst 定义,此方法可以抛出接口文档中描述的至少两个异常。

public void addFirst(E e) {
    if (e == null)
        throw new NullPointerException();
    elements[head = (head - 1) & (elements.length - 1)] = e;
    if (head == tail)
        doubleCapacity();
}

正如其他人提到的,接口上的 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:

private void doubleCapacity() {
    int n = elements.length;
    int newCapacity = n << 1;
    if (newCapacity < 0)
        throw new IllegalStateException("Sorry, deque too big");
}

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.

public void addFirst(E e) {
    if (e == null)
        throw new NullPointerException();
    elements[head = (head - 1) & (elements.length - 1)] = e;
    if (head == tail)
        doubleCapacity();
}

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.

可可 2024-11-07 07:31:06

唯一的例外是 ArrayDequeue.addFirst() 被记录为抛出NullPointerException。由于这是一个未经检查的异常,因此您不需要该 catch 块。

The only exception that ArrayDequeue.addFirst() is documented to throw is a NullPointerException. Since that is an unchecked exception, you don't need that catch-block.

病毒体 2024-11-07 07:31:06

Deque 的某些实现是绑定的(即容量有限),有些则不是。如果达到限制,诸如 addFirst 之类的方法会抛出 IllegalStateException。其他方法(例如 offerFirst)返回一个布尔值来指示完全相同的结果。

如果您不想处理潜在的异常,或者知道不会有任何异常,只需使用 offerFirst 并忽略结果。

Some implementations of Deque are bound (i.e. limited in capacity), some are not. Methods such as addFirst throw an IllegalStateException if the limit has been reached. Other methods such as offerFirst 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.

2024-11-07 07:31:06

Deque 会抛出 IllegalStateException,因为 Java 允许您使用不同的或创建自己的 Deque 实现,这可能有大小限制。 ArrayDeque 不会抛出这些异常,因此,如果您绝对确定您的代码将使用 ArrayDeque,那么就这样声明它们,它们将不会抛出 IllegalStateException >

A Deque throws IllegalStateException because Java allows you to use a different or create your own implementation of Deque, which may have size restrictions. ArrayDeque does not throw these exceptions, so if you are absolutely certain that your code will use ArrayDeque then declare them as such and they will not throw IllegalStateException

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文