1

I am implementing logic within a for-loop that will remove any dog objects with status "ACCEPTED" from a kennel Object.

Note that a Kennel can have a List of many dogs.

Loop:

allDogsInKennel = kennel.getDogsList();
for (int i = 0; i < allDogsInKennel.size(); i++) {
   //delete any dog object with a status of Accepted
   if (allDogsInKennel.get(i).getStatus() == "ACCEPTED") {
     kennel.removeDog(allDogsInKennel.get(i));
   }
}

removeDog method

 public void removeDog(Dog d) {
        this.dogList.remove(d);
 }

The problem I have is e.g. all 6 dogs in the list should be removed, but at present only 3 are being removed.

Example:

original size of list = 6 items

Item removed from index 0 = 5 items

Item removed from index 1 = 4 items

item removed from index 2 = 3 items

Now in the next iteration the loop tries to remove from index 3 due to i++ condition, but the array will only go to index 2 as it now has only 3 items at indexes:

0, 1, 2

How can I change my logic above to ensure that all items are removed from the array?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

1

You can add i-- when you delete an object from a list;

for (int i = 0; i < allDogsInKennel.size(); i++) {

    //delete any dog object with a status of Accepted
    if (allDogsInKennel.get(i).getStatus().equals("ACCEPTED")) {
        kennel.removeDog(allDogsInKennel.get(i));
        i--   
     }
}

Let's assume you have an arraylist and have string obj1, obj2, obj3

ArrayList<String> lst = new ArrayList<String>();
lst.add("obj1");
lst.add("obj2");
lst.add("obj3");

for (int i = 0; i < lst.size(); i++) {
     String str = lst.get(i);
     lst.remove(str); //list size decrease, 
     //so when you remove an object with index 0, your new list has obj2(at index 0) and obj3(at index 1) 
     //when i is increased, it will escape obj2, it never check it or access it.
     //i--; //open to give a try
}

for (int i = 0; i < lst.size(); i++) {
    System.out.println(lst.get(i)); //will print obj2
}

Expected is removing all items in the list, so I have to add i-- after remove method

Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
  • 1
    This seems to work, is it a proper solution or just a workaround? – java123999 Feb 29 '16 at 12:13
  • For example, if the list have 5 object and if you remove 3th object. now indices changes and previously 4th object's index is now 2(3th object on list). So I think it is a proper solution. For details I will edit answer and try to give more example – Yusuf K. Feb 29 '16 at 12:16
  • The proper solution would be to not use an ArrayList and a looped modification in the first place. In your case you can copy the remaining dogs to a new list. `n = ArrayList(dogList.size()); for { (d : dogList) if !d.getStatus().equals("ACCEPTED") n.add(d); } dogList = n;`. This gets cleaner with streams. – eckes Mar 06 '16 at 12:27
  • @eckes if it is important to hold original list, you are right(also this is a best practice), But if really need to remove these objects permanently, Isn't using original list more preferable? – Yusuf K. Mar 06 '16 at 19:22