从ArrayList中删除不起作用

The*_*tar 0 java arraylist

for(JCheckBox currentCheckBox : imagesToBeImportedCheckBox){ 
    if(currentCheckBox.isSelected()){
                    System.out.println("The text Box selected for removing are"+currentCheckBox.getText());
                }

             }
            for(JCheckBox currentCheckBox : imagesToBeImportedCheckBox){

                if(currentCheckBox.isSelected()){
                    System.out.println("I am entering in the loop where this image has to be removed "+currentCheckBox.getText());
                    imagesToBeImported.remove(currentCheckBox.getText());

                }

             }
            for(ResourceListObject currentImage : imagesToBeImported){

                System.out.println("After removing the images left are "+currentImage.getName());

             }
Run Code Online (Sandbox Code Playgroud)

这是输出

选择删除的文本框是aix71b

选择要删除的文本框是迁移图像

我进入循环,这个图像必须删除aix71b

我进入循环,必须删除此图像迁移图像

删除后剩下的图像是aix71b

删除图像后,左侧是迁移图像

Sud*_*hul 5

看看你的代码,很有可能,这就是问题所在.

imagesToBeImported.remove(currentCheckBox.getText());
Run Code Online (Sandbox Code Playgroud)

这会尝试从Collection中删除一个String(getText()提示我在这里说String)aix71b,imagesToBeImported但Collection imagesToBeImported包含了类型的元素ResourceListObject.

这就是为什么没有什么是从您的收藏为删除String和ResourceListObject是同一类型的不是.

编辑: -(您可以使用以下两种方式从列表中删除)

您可以遍历imagesToBeImported(使用迭代器)每个元素,imagesToBeImportedCheckBox并imagesToBeImported在resourceListObjectElement.getName()等于时删除元素currentCheckBox.getText().

或者,您可以根据其中的字段覆盖您的equals方法,以便您可以执行此类操作以将其从您的中删除.ResourceListObjectnameimagesToBeImported

imagesToBeImported.remove(new ResourceListObject(currentCheckBox.getText()));
// You need to add another constructor in your ResourceListObject class which takes in only the name field as the parameter.
Run Code Online (Sandbox Code Playgroud)