Java 如何从队列中获取特定元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22453755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:48:10  来源:igfitidea点击:

How to get a particular element from Queue?

javacollections

提问by user1612078

Unlike ArrayList, there is no get(int index)method in Queueto retrieve the element at specified position.

与 不同ArrayList,没有get(int index)方法Queue可以检索指定位置的元素。

Anybody please tell me how to achieve this in Queue?

有人请告诉我如何实现这一目标Queue

Thanks.

谢谢。

采纳答案by Vlad

You can remove elements from the Queue until you reach the needed one. You can re-add the removed elements at the end of the queue or put them in a different queue (and add the rest after you reached the needed element).

您可以从队列中删除元素,直到达到所需的元素。您可以在队列末尾重新添加已删除的元素或将它们放入不同的队列(并在到达所需元素后添加其余元素)。

You really shouldn't be using a Queue like that, though!

不过,您真的不应该使用这样的队列!

public static <T> T get(Queue<T> queue, int index) {
    synchronized (queue) {
        if (queue == null) {
            return null;
        }

        int size = queue.size();
        if (index < 0 || size < index + 1) {
            return null;
        }

        T element = null;
        for (int i = 0; i < size; i++) {
            if (i == index) {
                element = queue.remove();
            } else {
                queue.add(queue.remove());
            }
        }

        return element;     
    }
}

回答by RKC

Accessing elements by index is not part of the concept of a queue.

按索引访问元素不是队列概念的一部分。

If you need to access elements by index, you want a list, not a queue.

如果您需要按索引访问元素,您需要一个列表,而不是一个队列。

回答by MyPasswordIsLasercats

public static <T> T getFromQueue(Queue<T> queue, int index){
    if(index>=queue.size()){
        throw new IndexOutOfBoundsException("index="+index+",size="+queue.size());
    }
    Queue<T> queueCopy = new LinkedList<T>(queue);
    for(int i=0; i<index; i++){
        queueCopy.remove();
    }
    return queueCopy.peek();
}

回答by Rustam

public static Object retrieveElement(int index, Queue q) {
    Iterator it = q.iterator();
    int count = 0;
    while (it.hasNext()) {
        Object e = it.next();
        if (count == index) {
            it.remove();
            return e;
        }
        count++;
    }
    return null;
}

回答by staticx

private static  List<Individual> queueToList(Queue<Individual> archive) {
      List<Individual> list = new LinkedList<Individual>();
      Iterator<Individual> it = archive.iterator();
      while(it.hasNext()){
         list.add(it.next());
      }
    return list;
}

回答by Zachary Galica

Queues operate in a first in first out (FIFO) manner therefore in order to access a particular element the elements must be removed and stored in a secondary queue until the element being searched for is identified

队列以先进先出 (FIFO) 方式运行,因此为了访问特定元素,必须删除元素并将其存储在辅助队列中,直到识别出正在搜索的元素

回答by Nirmal Lamrin

Basically the concept of Queue is FIFO(First In First Out).So accessing the particular index or the element in the queue is not possible. The other way is to get a particular element you need to delete it unless and until you get it.

队列的概念基本上是 FIFO(先进先出)。因此访问特定索引或队列中的元素是不可能的。另一种方法是获取一个特定元素,您需要删除它,除非并且直到您得到它为止。