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

后端 未结 18 1628
感情败类
感情败类 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:47

    There is an easy way of removing all the null values from collection.You have to pass a collection containing null as a parameter to removeAll() method

    List s1=new ArrayList();
    s1.add(null);
    
    yourCollection.removeAll(s1);
    

提交回复
热议问题