Java Array Empty Check removeEmptyElements(String[] firstArray)

Here you can find the source of removeEmptyElements(String[] firstArray)

Description

Removes empty (or null) element from a given string array and returns a copy without thouse elements

License

Apache License

Parameter

Parameter Description
allElements a parameter

Declaration

public static String[] removeEmptyElements(String[] firstArray) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//w w w  . ja v a 2 s  .  c o m
     * Removes empty (or null) element from a given string array and returns a copy without thouse elements
     *
     * @param allElements
     * @return
     */
    public static String[] removeEmptyElements(String[] firstArray) {
        List<String> stringList = new ArrayList<String>();

        for (String string : firstArray) {
            if (string != null && string.trim().length() > 0) {
                stringList.add(string);
            }
        }

        firstArray = stringList.toArray(new String[stringList.size()]);
        return firstArray;

    }
}

Related

  1. isNullOrEmpty(T[] array)
  2. isNullOrEmptyOrFirstElementBlank(String[] values)
  3. normalizeTokens(String[] tokens, boolean removeEmpty)
  4. nullOrEmpty(Object[] array)
  5. removeEmpty(String[] strings)
  6. removeEmptyEntries(String[] array)
  7. removeEmptyLines(String lines[])
  8. removeEmptyString(final String[] words)
  9. removeEmptyString(String[] strOrigin)