Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-ArrayList remove()未删除_Java_Linux_Ubuntu_Arraylist - Fatal编程技术网

Java-ArrayList remove()未删除

Java-ArrayList remove()未删除,java,linux,ubuntu,arraylist,Java,Linux,Ubuntu,Arraylist,我对函数remove from ArrayList有问题。 我会尽量简短。 我有一个ArrayList,Posico是我创建的另一个类,我在数组中放置了一些对象。 问题是,当我打印ArrayList中的所有值时,我得到了预期值,但当我尝试使用removeobject或lastIndexOfvalue时,它不起作用。Remove不删除任何内容,lastIndexOf,甚至indexOf,我会收到一个错误,指出我请求的对象不在数组中。 我仔细检查了一下,发现物体确实在那里。 我做错什么了吗? 我遇到

我对函数remove from ArrayList有问题。 我会尽量简短。 我有一个ArrayList,Posico是我创建的另一个类,我在数组中放置了一些对象。 问题是,当我打印ArrayList中的所有值时,我得到了预期值,但当我尝试使用removeobject或lastIndexOfvalue时,它不起作用。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 :");

已经感谢您的耐心=

您的问题可能是您的类需要重写ObjecthashCode和Objectequals方法

这些方法是必须实现的,以便Java集合能够在需要查找某些元素、对它们自己和所有内容进行排序时正常工作

你可以阅读更多关于,关于

作为记录,我想,使用Eclipse或任何像样的Java IDE,您可以使用一些菜单项功能来实现这些:
Source->Generate hashCode and equals methods…

您能告诉我们pos变量的设置位置吗?在调用remove之前是否打印出pos以验证它不是逻辑错误…?等待tabuleiro0.posicoesLivres.containspos?pos是值还是位置?另一个函数设置pos,然后调用我打印的这个函数。我打印了pos,得到了正确的值,但找不到要删除的值。Pos是我在Posicao类中创建的一个对象,它有2个整数,x和y。谢谢你。我这么做了,但没用。当我使用remove、indexOf或lastIndexOf时,它们都找不到值。但如果我按索引删除,我知道这是第一个有效的方法。