Alex Rivera | Logout

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

Asked 2011-01-27T17:21:07.220
220

I try with a loop like that

// ArrayList tourists

for (Tourist t : tourists) {
    if (t != null) {     
        t.setId(idForm); 
    }   
}

But it isn't nice. Can anyone suggest me a better solution?


Some useful benchmarks to make better decision:

While loop, For loop and Iterator Performance Test

Edit
Report

1 Answer

1

I played around with this and found out that trimToSize() seems to work. I am working on the Android platform so it might be different.

answered 2011-05-19T10:00:53.357

Your Answer