我想编写一个获取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)
但是,它随时删除队列的第一个变量......为什么?