Samth
Samth

Reputation: 113

Java - LinkedList Doubt

I need to create a FIFO queue. I was thinking in creating a LinkedList for that, because of it's native methods to remove and add. But my queue should have a fixed size, so how could I fix that size?

Thanks in advance!

Upvotes: 0

Views: 149

Answers (3)

joeslice
joeslice

Reputation: 3454

If you must have a fixed size, then you ought to use an ArrayList (or just an array) to back the FIFO.... Just keep a variable representing the index of the head, and a variable representing the index of the tail and move them around as you push and pop.

However, if this isn't homework, you should probably just use one of the many available Collections classes. They do the job very well.

Upvotes: 1

lobster1234
lobster1234

Reputation: 7779

You can wrap an instance of a LinkedList in your own class, and control the size (composition). The downside (or upside, based on your preference) of this is that you can control which methods to explose, in this case add and remove. Another alternative is to extend LinkedList and override add/remove while controlling the size.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234807

The easiest thing would be use one of the implementations of java.util.Deque or java.util.Queue

Upvotes: 6

Related Questions