小编Goe*_*ber的帖子

使用Java从队列中删除var

我想编写一个获取Queue和参数(int)的函数,该函数删除Queue中与参数相等的var.这是我的代码:

 public static void remove_it( Queue <Integer> q, int x)
{

    Queue <Integer> temp = new LinkedList<Integer>();
    boolean found = false;
    if (!q.isEmpty())
    {
        if (q.peek()==x)
            found = true;
        temp.add(q.remove());
    }

   boolean b;
    if (found)
    {
     b = true;

    while (!temp.isEmpty())
    {
        if (temp.peek() == x  && b)
            {
                temp.remove();
                b = false;
            }
        else
            q.add(temp.remove());
    }
   }

    }
Run Code Online (Sandbox Code Playgroud)

但是,它随时删除队列的第一个变量......为什么?

java linked-list

1
推荐指数
1
解决办法
53
查看次数

标签 统计

java ×1

linked-list ×1