-4

Here my Code:

int ii = 0;
HTMLOption[] options = new HTMLOption[zoneList.size()];
for (AuditPlanSch zone : zoneList) {
    System.out.println("Before Temp====" + tmp.getZoneCode() + "---list-" + zone.getZoneCode() + "----iii" + ii);
    if (tmp.getZoneCode().equals(zone.getZoneCode())) {
        System.out.println("Temp==2==" + tmp.getZoneCode() + "---list-2-" + zone.getZoneCode());
        zoneList.remove(zone.getZoneCode());
    }
    System.out.println("After Temp====" + tmp.getZoneCode() + "---list--" + zone.getZoneCode() + "----iii" + ii);
    options[ii++] = new HTMLOption(zone.getZoneCode(), zone.getZoneName(), false);

}

the output:

Before Temp====14751---list--14751----iii1
Temp==2==14751---list-2-14751

After Temp====14751---list--14751----iii1

Before Temp====14751---list--14752----iii2

After Temp====14751---list--14752----iii2

Before Temp====14751---list--14754----iii3

After Temp====14751---list--14754----iii3

Before Temp====14751---list--14756----iii4

After Temp====14751---list--14756----iii4

Before Temp====14751---list--15844----iii5

After Temp====14751---list--15844----iii5

Before Temp====14751---list--M6205----iii6

After Temp====14751---list--M6205----iii6

the equal value not remove from the list.

GameDroids
  • 5,584
  • 6
  • 40
  • 59

2 Answers2

1

Your zoneList is a list of AuditPlanSch, but at zoneList.remove(zone.getZoneCode()); you are trying to remove the code (which is an Integer or String I assume).

So one change is: zoneList.remove(zone.getZoneCode()); should be zoneList.remove(zone);.

This will however give a ConcurrentModificationException because you are trying to remove items from the list you are for-eaching over. So, instead change it to this:

for(Iterator<AuditPlanSch> it = zoneList.iterator(); it.hasNext(); ) {
  AuditPlanSch zone = it.next();
  if (tmp.getZoneCode().equals(zone.getZoneCode())) { 
    it.remove();
  }
  options[ii++] = new HTMLOption(zone.getZoneCode(), zone.getZoneName(), false);
}

EDIT: In addition, your print lines doesn't make much sense, since even though you've removed it from the list, you are still accessing the zone and tmp in the variables.

To see that the values are removed from the list as expected, try printing the entire list.

In addition, I'm suspecting you want to add null to the HTMLOption when removed, so I've added that to the code as well:

for(Iterator<AuditPlanSch> it = zoneList.iterator(); it.hasNext(); ) {
  System.out.println("List before: "+zoneList);
  AuditPlanSch zone = it.next();
  String zoneCode = zone.getZoneCode();
  String zoneName = zone.getZoneName();
  if (tmp.getZoneCode().equals(zone.getZoneCode())) {
    System.out.println("Remove zone with code "+zone.getZoneCode());
    it.remove();
    zoneCode = null;
    zoneName = null;
  }
  System.out.println("List after: "+zoneList);
  options[ii++] = new HTMLOption(zoneCode, zoneName, false);
}
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • not working, it's showing error like , java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:939) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:893) – Edwin Benjamin Aug 28 '18 at 10:14
  • error is showing in the for loop line – Edwin Benjamin Aug 28 '18 at 10:15
  • and the both beans are String value only, not a Integer – Edwin Benjamin Aug 28 '18 at 10:16
  • @EdwinBenjamin I've modified the code, it indeed contained a mistake. `zoneList.remove(zone);` should have been `it.remove();` when using the iterator. Could you try it again to see if it now works? – Kevin Cruijssen Aug 28 '18 at 10:37
  • @ Kevin:Still that value not remove,Before Temp====14751---list--14751----iii1 after Temp====14751---list--14751----iii1 – Edwin Benjamin Aug 28 '18 at 10:42
  • @EdwinBenjamin That's because you are printing the item itself, not the list to see the changes. The `zone` and `tmp` are variables that remain in memory even though you've removed it from the list. I've added an _EDIT_ section which prints the entire list before and after to see it does correctly get removed from the list as expected. – Kevin Cruijssen Aug 28 '18 at 10:50
0

First, you are not using the correct instance type to remove for this list.

List.remove(Object)

Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

So you need to pass an instance that can be compared with the instance in your list :

zoneList.remove(zone);

Second, AuditPlanSch must implement equals(Object) method. This is needed for List.remove(Object) to find the instance to remove. (As the documentation mentionned)

Last, you need to use an iterator to be able to update a list will iterating (or by index, but iterator is simpler) or you will get a concurrent modification exception.


Of course, you could go with a much simpler way, using the Collection.removeif method:

final tmp ...
list.removeIf(zone -> tmp.getZoneCode().equals(zone.getZoneCode()))

This will simple remove every instance zone where tmp.getZoneCode().equals(zone.getZoneCode()) is true. Note that tmp must be final (or effectively final) for this lambda expression to compile.

The advantage here is that you don't have to make sur the instance implements equals properly, you define the condition to use.

AxelH
  • 14,325
  • 2
  • 25
  • 55