简体   繁体   中英

Enhanced for loop to remove objects from ArrayList

I have two ArrayLists from a Parse Query that I am comparing with an enhanced for loop. I want to check to see if there is a matching ObjectId in the 2 Lists, if so, I want to remove that Object from "allDropList". All other components are working properly, I believe the problem to be with my enhanced for loop syntax. Here is my code, thanks! Note: I have experimented with "if" and "while", with no success.

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;
}

The problem is you can't iterate over a list and remove something at the same time with a for loop. Have a look here for further explanation.

Removing objects in list is safer is done in a reverse order:

  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;
  }

Or you can just simply do this:

    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;
}

If you use an Iterator instead for the outer loop, you can remove items from the list while iterating, without resorting to other 'tricks'.

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;
}

Here's the corrected answer using an Iterator. This is a full example that compiles and runs:

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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM