4

I would like to know what this tutorial means when it refers to the following bit of explanation. In particular the part which I highlighted in bold.

Insert

The addfirst and offerFirst methods insert elements at the beginning of the Deque instance. The methods addLast and offerLast insert elements at the end of the Deque instance. When the capacity of the Deque instance is restricted, the preferred methods are offerFirst and offerLast because addFirst might fail to throw an exception if it is full.

  1. Why would offerFirst be preferred?
  2. Why would addFirst fail to throw an exception if it is full? Should not it be better if it guaranteed to throw an exception in those circumstances?
dimo414
  • 47,227
  • 18
  • 148
  • 244
Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • This is not what the linked tutorial says. The last "offerFirst" should be "addFirst". Was this corrected? – xehpuk May 09 '15 at 13:39

5 Answers5

1

I think both methods are legitimate (though the offerXXX methods are more likely to be used in bounded dequeues).

If your code assumes that there's available space in the queue, and this assumption is critical to the correctness of the code, use addFirst/addLast. The runtime exception being thrown (IllegalStateException) is perfectly suitable for this bug scenario.

If, on the other hand, a full queue is a normal scenario, don't deal with it using exceptions. Use offerFirst/offerLast, and check the returned value.

Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78
0

OfferFirst is the prefereable methods if there is a risk the deque will reach capacity. If it has reached capacity addFirst will throw an exception, where as offerFirst returns a boolean (true/false) value to indicate if the add was successful. offerFirst Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addFirst(E) method, which can fail to insert an element only by throwing an exception.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

Why would be that when using the restricted version you would not want an exception thrown when adding the element fails. This is because you would expect some failures which is why you are offering to add instead of insisting to add.

onesixtyfourth
  • 744
  • 9
  • 30
0

It means that the offerXXX methods return boolean, and the addXXX methods don't.

So it is recommending that you use offerXXX and check the boolean for success, rather than expecting an exception to be thrown from either method.

It's very badly worded. And so is the Javadoc.

David Lavender
  • 8,021
  • 3
  • 35
  • 55
-1

According to Docs:

offerFirst:

Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addFirst(E) method, which can fail to insert an element only by throwing an exception.

Which means if you use addFirst with a capacity restricted deque, it may throw an exception, but using offerFirst won't throw any exception.

offerLast

Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addLast(E) method, which can fail to insert an element only by throwing an exception.

Similarly if you use addLast with a capacity restricted deque, it may throw an exception, but using offerLast won't throw any exception.

Apurv
  • 3,723
  • 3
  • 30
  • 51
  • 1
    I dont think it answers OP question. His question is WHY? – Achintya Jha Mar 26 '13 at 14:39
  • _Which means if you use addFirst with a capacity restricted deque, it may throw an exception, but using offerFirst won't throw any exception._ (as posted above) – Apurv Mar 26 '13 at 14:39
  • 1
    @Apurv: that's not a complete explanation. Avoiding exceptions is not always wise. See my response (http://stackoverflow.com/questions/15639651/insertion-on-a-deque-in-java/15639768#15639768) – Eyal Schneider Mar 26 '13 at 14:53