How to remove all null elements from a ArrayList or String Array?

后端 未结 18 1663
感情败类
感情败类 2020-11-27 10:26

I try with a loop like that

// ArrayList tourists

for (Tourist t : tourists) {
    if (t != null) {     
        t.setId(idForm); 
    }   
}
18条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 10:52

    We can use iterator for the same to remove all the null values.

    Iterator itr= tourists.iterator();
    while(itr.hasNext()){
        if(itr.next() == null){
            itr.remove();
        }
    }
    

提交回复
热议问题