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

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

    Try:

    tourists.removeAll(Collections.singleton(null));
    

    Read the Java API. The code will throw java.lang.UnsupportedOperationException for immutable lists (such as created with Arrays.asList); see this answer for more details.

提交回复
热议问题