开发者

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 h开发者_JS百科ow could I fix that size?

Thanks in advance!


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜