-2

I have to remove the Object if the value is 0. By using this code, only several were removed, but some were not.

public static List<DataEntityInteger> removeZeroValue(List<DataEntityInteger> list) {                
    for (int a=0; a<list.size(); a++) {                                                              
        DataEntityInteger entityInteger = list.get(a);                                               
        if (entityInteger.getValue() == 0) {                                                         
            list.remove(a);                                                                          
        }                                                                                            
    }                                                                                                
    return list;                                                                                     
}    

DataEntityInteger class:

public class DataEntityInteger {                


    @SerializedName("keyname")                  
    @Expose                                     
    private String keyname;                     
    @SerializedName("value")                    
    @Expose                                     
    private Integer value;                      

    public String getKeyname() {                
        return keyname;                         
    }                                           

    public void setKeyname(String keyname) {    
        this.keyname = keyname;                 
    }                                           

    public Integer getValue() {                 
        return value;                           
    }                                           

    public void setValue(Integer value) {       
        this.value = value;                     
    }    
}                 

So my question is, how can I remove all the zero-valued object in the ArrayList?

user207421
  • 305,947
  • 44
  • 307
  • 483
Zarul Izham
  • 569
  • 5
  • 17

2 Answers2

5

When removing an element from the List, you must decrement the loop index, since removing an element decreases the indices of the elements following the removed element :

public static List<DataEntityInteger> removeZeroValue(List<DataEntityInteger> list) {                
    for (int a=0; a<list.size(); a++) {                                                              
        DataEntityInteger entityInteger = list.get(a);                                               
        if (entityInteger.getValue().equals(0)) {                                                         
            list.remove(a);   
            a--;                                                                       
        }                                                                                            
    }                                                                                                
    return list;                                                                                     
}  

Alternately you can use an Iterator and it's remove() method.

public static List<DataEntityInteger> removeZeroValue(List<DataEntityInteger> list) {   
    Iterator<DataEntityInteger> iter = list.iterator();          
    while (iter.hasNext()) {                                                              
        DataEntityInteger entityInteger = iter.next();                                               
        if (entityInteger.getValue().equals(0)) {                                                         
            iter.remove();                                                                       
        }                                                                                            
    }                                                                                                
    return list;                                                                                     
}

Also, since your getValue() method is an Integer and not a primitive int, you should compare it to 0 with equals(), not with ==.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • @Blackbelt In what way? – Eran May 08 '17 at 11:10
  • @Blackbelt No, why do you think so? – user207421 May 08 '17 at 11:10
  • @Blackbelt That's only an issue if you use the enhanced for loop (or an explicit Iterator without using the Iterator's remove() method). – Eran May 08 '17 at 11:13
  • @Blackbelt So you must therefore believe that `Iterator.remove()` is entirely useless? You don't seem to know much about it. – user207421 May 08 '17 at 11:13
  • @Blackbelt That's not correct. It is only true *if* you are iterating, which neither the OP nor the first suggestion in this answer is doing; but it is also exactly what the second suggestion in this answer is doing. Have you read it? – user207421 May 08 '17 at 11:17
  • @EJP I think he is referring to my first suggestion, which doesn't use Iterator's remove(). – Eran May 08 '17 at 11:18
  • @Blackbelt Then you are still wrong. About everything. The OP's code, the duplicate, the first suggestion, and the second suggestion. – user207421 May 08 '17 at 11:21
  • @Blackbelt I have already done so above. Please take the trouble to try to verify your suggestions and comments before posting again. – user207421 May 08 '17 at 11:22
  • Iterator is great, I think I will use first one, less variable used. Thank you guys. – Zarul Izham May 08 '17 at 11:58
2

you have several way to fix your problem, one of those is reverse iteration through your list,

normal iteration have an issue because each time you remove item all index will be changed.

for example think you have list of following item:

{1, 3, 1, 1, 4, 1, 1 }

and you want remove each item that value is 1

with normal for loop you check just following position in source list:

0,2,4,5.

why?

first check is true so your new list will be:

{ 3, 1, 1, 4, 1, 1 }

in next step your index is 1 and you simply skip 3 because you have change source list.

if you go on check every time you want to remove one item from list you will skip next item,

so your code must be like follow:

for (int a=list.size()-1 ; a >= 0; a--) {                                                              
        DataEntityInteger entityInteger = list.get(a);                                               
        if (entityInteger.getValue() == 0) {                                                         
            list.remove(a);                                                                          
        }                                                                                            
    } 

or you can change for statement index in each remove like:

for (int a=0; a<list.size(); a++) {                                                              
        DataEntityInteger entityInteger = list.get(a);                                               
        if (entityInteger.getValue() == 0) {                                                         
            list.remove(a);  
            a--;                                                                        
        }                                                                                            
    }        

and as @Eran mentioned use .equals() instead of ==

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63