Reputation: 305
I'm trying to implement my own double-ended queue using ArrayList, and I know how to add elements to the front/back if the capacity is some number more than zero.
But I'm trying to figure out how to addFirst if the capacity of the list is zero. Is it even possible? Considering for my case the size of the array is already determined in the beginning and cannot be changed. It's what my homework instructions tells me to do.
Upvotes: 0
Views: 296
Reputation: 10945
If someone tries to add to a queue whose capacity is zero, or in fact any time someone tries to add an element which would exceed the capacity of the queue, you should throw an IllegalStateException
.
Personally I would recommend taking a look at AbstractQueue
and its subclasses in the API docs for a good example of how to handle the various error other conditions as well.
Upvotes: 2
Reputation: 19546
If the queue is full (eg: capacity of zero) I would just throw an exception saying that the queue is full. Or return a value to indicate that something went wrong.
Upvotes: 1