CircularFIFOQueue:使用 get 方法

CircularFIFOQueue : Using get method

我正在尝试在 java 中实现 CircularFifoQueue。

Queue<List<String>> rssififo = new CircularFifoQueue<List<String>>(2);

我对如何使用 CircularFifoQueue 的 get 方法感到困惑 API。 API 表示

public E get(int index)
Returns the element at the specified position in this queue.
Parameters:
index - the position of the element in the queue
Returns:
the element at position index
Throws:
NoSuchElementException - if the requested position is outside the range [0, size)

但是我似乎无法使用 rssiinfo.get(index)。在我的编辑器 (android studio) 中弹出 get 方法的唯一方法是如果我使用 rssiinfo.element().get(0).

但是 element() 方法仅 returns 队列的第一个元素。如何访问队列中任意位置的元素?

您的变量 rssififo 是通用 Queue 类型而不是具体类型 CircularFifoQueue
Queue接口没有get方法,只有peek、poll和remove方法。 如果将声明更改为 CircularFifoQueue,则可以访问 get 方法。

您必须将密码更改为

CircularFifoQueue<List<String>> rssififo = new CircularFifoQueue<List<String>>(2);

CircularFifoQueue 有方法 get(int index)。但是 CircularFifoQueue 实现了 Queue 接口。该接口没有方法 get(int index)。这就是您无法使用它的原因。