How to remove null value from String array in java?
String[] firstArray = {"test1","","test2","test4",""};
I need the "firstArray" without null ( empty) values like this
String[] firstArray = {"test1","test2","test4"};
If you want to avoid fencepost errors and avoid moving and deleting items in an array, here is a somewhat verbose solution that uses
Added |
||||
|
If you actually want to add/remove items from an array, may I suggest a
Then, if you really need to put that back into an array:
|
|||||||||||
|
Those are zero-length strings, not null. But if you want to remove them:
You can move the second into the first thusly:
If you were to do this for elements [1,2], then [2,3], etc. you would eventually shift the entire contents of the array to the left, eliminating element 0. Can you see how that would apply? |
|||||||||||||
|
Using Google's guava library
|
|||||||||
|
null
is completely different from "empty string" in Java. That's your first problem. – JSBձոգչ Nov 10 '10 at 23:55