java - 使用 Java 从队列中删除 var
问题描述
我想写一个获取队列和参数(int)的函数,该函数删除队列中等于参数的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());
}
}
}
但是,它随时删除队列的第一个变量......这是为什么?
解决方案
要修复您的错误,请将其全部替换为一行:
public static void remove_it(Queue <Integer> q, int x) {
q.remove(x);
}
推荐阅读
- r - RShiny 中的反应性数据问题
- amazon-web-services - Localstack:函数已经存在/ResourceConflictException
- c# - 如何在 C# 中测试 setter?为什么?
- javascript - 如何设置 Babel,然后在命令提示符下点击“npm run dev”,而不会出现“./src/js/index.js 中的错误”?
- javascript - service.foo 不是 Angular 的函数
- swift - 有没有更好的方法将信息发送到子视图?
- c# - 在 Docker 容器中运行 SQL Lite 内存单元测试时出错
- sql - 如何在 Rails 中回滚所有最近的事务
- gatsby - 砌体中的盖茨比图像没有高度
- nuxt.js - 在 Nuxt 配置中设置自定义主机不起作用