add方法与offer方法的不同之处是:
你看:
boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.
它们的区别就是add 方法在插入失败的时候会抛出 IllegalStateException 异常
而offer可以通过返回值来判断成功与否。
同理,remove方法在移除失败的时候会抛出 IllegalStateException 异常
而poll方法可以通过返回值来判断成功与否。
希望可以帮到你。
|