简体   繁体   English

Java-LinkedList怀疑

[英]Java - LinkedList Doubt

I need to create a FIFO queue. 我需要创建一个FIFO队列。 I was thinking in creating a LinkedList for that, because of it's native methods to remove and add. 我正在考虑为此创建一个LinkedList,因为它是删除和添加的本机方法。 But my queue should have a fixed size, so how could I fix that size? 但是我的队列应该有固定的大小,那么如何确定该大小呢?

Thanks in advance! 提前致谢!

最简单的方法是使用java.util.Dequejava.util.Queue的实现之一

You can wrap an instance of a LinkedList in your own class, and control the size (composition). 您可以在自己的类中包装LinkedList的实例,并控制大小(组成)。 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 . 不利的一面(或基于您的偏好)是,您可以控制要使用的方法,在这种情况下,请addremove Another alternative is to extend LinkedList and override add / remove while controlling the size. 另一种选择是扩展LinkedList并在控制大小的同时覆盖add / remove

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. 如果必须具有固定的大小,则应使用ArrayList(或仅一个数组)来支持FIFO。...只需保留一个代表头部索引的变量,以及一个代表头部索引和尾部索引的变量。按下并弹出时将它们移动。

However, if this isn't homework, you should probably just use one of the many available Collections classes. 但是,如果这不是家庭作业,则可能应该只使用许多可用的Collections类之一。 They do the job very well. 他们做得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM