How to get rid of empty element of the array in Java?

Lets say that I have an array as followed :

  String[] letter = {a, b, c, e, f, }

How can I trim this array in order to get rid the empty element?

Answers


There is no "empty" element in that array. The trailing comma makes no difference - your array still has 5 elements.

If you have a real problem in real code where some elements in an array are empty (and you'll have to say what you mean by that - null value? Value referring to an empty string?) you can't change the size of an existing array, but you can create a new array, e.g. with

List<String> list = new ArrayList<String>();
for (String text : array)
{
    if (text != null && text.length() > 0)
    {
        list.add(text);
    }
}
array = list.toArray(new String[0]);

Need Your Help

StAX Parsing - where to keep element schemas?

java xml parsing configuration stax

I need to write a parser to save data from XML in the format:

What (self.nsmutablearray) means?

ios objective-c uitableview

I'm developing a contact list for studying purposes and this question came while studying.