Java - ArrayList remove() 未删除

标签 java linux ubuntu arraylist

我在使用 ArrayList 中的 remove() 函数时遇到问题。 我会尽量简短。 我有一个 ArrayList,Posicao 是我创建的另一个类,我在数组中放入了一些对象。 问题是,当我打印 ArrayList 中的所有值时,我得到了预期的结果,但是当我尝试使用 remove(object) 或 lastIndexOf(value) 时,它不起作用。 Remove() 不会删除任何内容,lastIndexOf() 甚至是indexOf(),我收到一条错误消息,指出我请求的对象不在数组中。 我仔细检查了一下,该对象实际上就在那里。 我做错了什么吗? 我遇到问题的代码部分。 如果还有其他需要我发布的内容,请询问。 (=

System.out.println("BEGIN - Print beforeremove:" + tabuleiro0.posicoesLivres.contains(pos));
    for(int i = 0 ; i < tabuleiro0.posicoesLivres.size() ; i++)
        System.out.println(((Posicao)tabuleiro0.posicoesLivres.get(i)).x + " " + ((Posicao)tabuleiro0.posicoesLivres.get(i)).y);
    System.out.println("END - Print before remove :");

    tabuleiro0.posicoesLivres.remove(pos);

    System.out.println("BEGIN - Print after remove:" + tabuleiro0.posicoesLivres.contains(pos));
    for(int i = 0 ; i < tabuleiro0.posicoesLivres.size() ; i++)
        System.out.println(((Posicao)tabuleiro0.posicoesLivres.get(i)).x + " " + ((Posicao)tabuleiro0.posicoesLivres.get(i)).y);
    System.out.println("END - Print after remove :");

感谢您的耐心等待 (=

最佳答案

您的问题可能是您的类需要覆盖 Object#hashCodeObject#equals 方法。

必须实现这些方法,以便 Java Collections 在必须查找某些元素、对自身进行排序以及所有内容时能够正常工作...

您可以阅读有关 when do I need to override equals and hashcode methods? 的更多信息,以及关于overriding equals and hashCode in Java ;)

郑重声明,使用Eclipse(或者我猜任何像样的Java IDE),您可以使用一些菜单项功能来实现这些: 源 -> 生成 hashCode() 和 equals() 方法...

关于Java - ArrayList remove() 未删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22952058/

相关文章:

java - 在我的程序中,在执行 SimpleDateFormat.parse 时间歇性地出现 ParseException

java - 词法分析只给出一个输出?

同时为 2 个文件夹编写 Linux bash 脚本

linux - 如何保证 ubuntu 13.10 中 shell 脚本的顺序运行?

linux - Bash 脚本 : For-Loop Over Newline. .. 语法解释?

linux - 获取安装到/dev 文件夹的 USB 设备的唯一序列号

linux - 在 ubuntu live disk 中运行 docker

java - 在不配对的情况下使用带 BLE 的 Rfcomm 套接字?

java - 如何从 Java 中的集合中弹出项目?

ubuntu 18.04 LTS/usr/lib/fresh install 中的 python 目录