Java 用于从ArrayList中删除对象的增强for循环

Java 用于从ArrayList中删除对象的增强for循环,java,android,parsing,for-loop,arraylist,Java,Android,Parsing,For Loop,Arraylist,我将解析查询中的两个ArrayList与增强的for循环进行比较。我想检查2个列表中是否有匹配的ObjectId,如果有,我想从“allDropList”中删除该对象。所有其他组件都工作正常,我认为问题在于我的增强for循环语法。这是我的密码,谢谢!注意:我尝试过“如果”和“虽然”,但没有成功 public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList , ArrayList &l

我将解析查询中的两个ArrayList与增强的for循环进行比较。我想检查2个列表中是否有匹配的ObjectId,如果有,我想从“allDropList”中删除该对象。所有其他组件都工作正常,我认为问题在于我的增强for循环语法。这是我的密码,谢谢!注意:我尝试过“如果”和“虽然”,但没有成功

public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList , ArrayList <DropItem> allDropsList){

    for(DropItem dropItemAll : allDropsList) {

        for(DropItem dropItemRelation  : hasRelationList) {

           /*if*/ while(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId())) {
                  allDropsList.remove(dropItemAll);
            }
        }
    }
    return allDropsList;
}
public ArrayList filterDrops(ArrayList hasRelationList,ArrayList allDropsList){
对于(DropItem dropItemAll:allDropsList){
for(DropItem dropItemRelation:hasRelationList){
/*if*/while(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId())){
allDropsList.remove(dropItemAll);
}
}
}
返回allDropsList;
}

问题在于,您不能使用
for
循环在遍历列表的同时删除某些内容。查看更多说明。

按相反顺序删除列表中的对象更安全:

  public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList ,ArrayList <DropItem> allDropsList){
    for(int i = allDropsList.size()-1; i >=0; i--) {
      for(int j = hasRelationList.size()-1; j >=0; j--) {
        if(allDropsList.get(i).getObjectId().equals(hasRelationList.get(j).getObjectId())) {
          allDropsList.remove(i);
        }
      }
    }
    return allDropsList;
  }
public ArrayList filterDrops(ArrayList hasRelationList,ArrayList allDropsList){
对于(int i=allDropsList.size()-1;i>=0;i--){
对于(int j=hasRelationList.size()-1;j>=0;j--){
if(allDropsList.get(i).getObjectId().equals(hasRelationList.get(j).getObjectId())){
全部删除列表。删除(i);
}
}
}
返回allDropsList;
}

或者您只需执行以下操作:

    public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList , ArrayList <DropItem> allDropsList){

    ArrayList<DropItem> temp = new ArrayList<DropItem>(); //place the objects you want inside this arraylist
    for(DropItem dropItemAll : allDropsList) {
        int counter = 0;
        for(DropItem dropItemRelation  : hasRelationList) {

            if(dropItemAll.getId().equals(dropItemRelation.getId())) {
                break;
            }
            if(counter == hasRelationList.size()-1){
               temp.add(dropItemAll);
            }
            counter++;
        }
    }
    return temp;
}
public ArrayList filterDrops(ArrayList hasRelationList,ArrayList allDropsList){
ArrayList temp=new ArrayList();//将所需的对象放置在此ArrayList中
对于(DropItem dropItemAll:allDropsList){
int计数器=0;
for(DropItem dropItemRelation:hasRelationList){
if(dropItemAll.getId().equals(dropItemRelation.getId())){
打破
}
if(计数器==hasRelationList.size()-1){
临时添加(dropItemAll);
}
计数器++;
}
}
返回温度;
}

如果对外部循环使用迭代器,则可以在迭代时从列表中删除项,而无需使用其他“技巧”

public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList, ArrayList <DropItem> allDropsList){
    Iterator<DropItem> allDropsIterator = allDropsList.iterator();

    while(allDropsIterator.hasNext()) {
        DropItem dropItemAll = allDropsIterator.next();

        for(DropItem dropItemRelation  : hasRelationList) {
          if(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId()){
              allDropsIterator.remove(dropItemAll);
        }
    }

    return allDropsList;
}
public ArrayList filterDrops(ArrayList hasRelationList,ArrayList allDropsList){
迭代器allDropsIterator=allDropsList.Iterator();
while(allDropsIterator.hasNext()){
DropItem dropItemAll=allDropsIterator.next();
for(DropItem dropItemRelation:hasRelationList){
if(dropItemAll.getObjectId().equals)(dropItemRelation.getObjectId()){
allDropsIterator.remove(dropItemAll);
}
}
返回allDropsList;
}

以下是使用迭代器更正的答案。 这是一个完整的编译和运行示例:

import java.util.ArrayList;
import java.util.Iterator;

 public class DropTest {

    class DropItem {
       private String objectId;

       public String getObjectId() {
          return objectId;
       }

       public DropItem(String id) {
          this.objectId = id;
       }
    }

    public static void main(String[] args) {
        new DropTest().test();
    }

    public void test() {
        ArrayList<DropItem> allDropsList = new ArrayList<>();       
        allDropsList.add(new DropItem("one"));
        allDropsList.add(new DropItem("two"));
        allDropsList.add(new DropItem("three"));
        allDropsList.add(new DropItem("four"));
        allDropsList.add(new DropItem("five"));

        ArrayList<DropItem> hasReleationList = new ArrayList<>();
        hasReleationList.add(new DropItem("three"));
        hasReleationList.add(new DropItem("five"));

        System.out.println("before:");
        for(DropItem dropItem : allDropsList) {
           System.out.println(dropItem.getObjectId());
        }

        ArrayList<DropItem> result = filterDrops(hasReleationList, allDropsList);
        System.out.println("\n\nafter:");
        for(DropItem dropItem : result) {
           System.out.println(dropItem.getObjectId());
        }
   }

   public ArrayList<DropItem> filterDrops(ArrayList <DropItem> hasRelationList, ArrayList <DropItem> allDropsList) {
       Iterator<DropItem> allDropsIterator = allDropsList.iterator();

       while(allDropsIterator.hasNext()) {
          DropItem dropItemAll = allDropsIterator.next();

          for(DropItem dropItemRelation  : hasRelationList) {
              if(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId())){
                  allDropsIterator.remove();
              }
          }
      }

      return allDropsList;
   }
}
import java.util.ArrayList;
导入java.util.Iterator;
公共类辍学考试{
类DropItem{
私有字符串objectId;
公共字符串getObjectId(){
返回objectId;
}
公共DropItem(字符串id){
this.objectId=id;
}
}
公共静态void main(字符串[]args){
新的DropTest().test();
}
公开无效测试(){
ArrayList allDropsList=新的ArrayList();
allDropsList.add(新的DropItem(“一”));
allDropsList.add(新的DropItem(“两”));
allDropsList.add(新的DropItem(“三”));
allDropsList.add(新的DropItem(“四”));
allDropsList.add(新增DropItem(“五”));
ArrayList HasRelationList=新建ArrayList();
添加(新的DropItem(“三”));
hasRelationList.add(新的DropItem(“五”));
System.out.println(“之前:”);
对于(DropItem DropItem:allDropsList){
System.out.println(dropItem.getObjectId());
}
ArrayList结果=filterDrops(HasRelationList,allDropsList);
System.out.println(“\n\n后面:”);
for(DropItem:result){
System.out.println(dropItem.getObjectId());
}
}
公共ArrayList filterDrops(ArrayList hasRelationList、ArrayList allDropsList){
迭代器allDropsIterator=allDropsList.Iterator();
while(allDropsIterator.hasNext()){
DropItem dropItemAll=allDropsIterator.next();
for(DropItem dropItemRelation:hasRelationList){
if(dropItemAll.getObjectId().equals(dropItemRelation.getObjectId())){
allDropsIterator.remove();
}
}
}
返回allDropsList;
}
}

你尝试过以相反的顺序使用simple for循环吗?我没有,你介意解释一下你的意思吗?谢谢!而且,如果我理解得很好,
在这里是胡说八道的。
如果应该使用
你不应该使用for循环从列表中删除项目——而是使用迭代器。我会创建不同的ArrayList查看结果并在其中添加项目,以便稍后返回,而不是删除项目。我看到了,Frank。在这种情况下,你有什么建议?@Kevin Hodges看一看感谢链接Frank,我将检查此链接。感谢你的输入buz。我实现了此代码,不幸的是,相关的删除仍然没有从我的allDropsList中删除。好的。你看到了吗通过调试检查内部测试是否正常运行?感谢您的输入,我收到了:“迭代器中的remove()不能应用于”您应该在迭代器(迭代器)上调用remove(),而不是在DropItem上。如果您这样做,那么肯定还有另一个问题,因为迭代器肯定支持remove(T)method.dropItemAll是一个,这不允许我使用您的代码,因为“remove(dropItemAll)”;是非法的。有没有办法解决这个问题?dropItemAll不是一个,因为从语法上讲,没有这样的东西。dropItem是一个dropItem。AllDropSiteerator是一个迭代器,它肯定有remove(dropItem)方法。如果您仍然有问题,请发布您当前的代码