If you are looking for a high performance solution, I think this is the best solution. Otherwise, if your input array is not so huge, I would use a solution similar to Peter Lawrey, so your code is easy to understand.
With this solution, you only loop the input array, and if you do not need the input array, you can avoid one instance of the copy instance that filterBlankLines called with preserveInput = false.
public class CopyingStringArrayIntoNewStringArray { public static void main(String[] args) { String[] str = { "", "1", "", null, "2", " ", "3", "" }; System.out.println("\nBefore:"); printArrays(str); String[] xml = filterBlankLines(str, true); System.out.println("\nAfter:"); printArrays(xml); } private static String[] filterBlankLines(String input[], boolean preserveInput ) { String[] str; if (preserveInput) { str = new String[input.length]; System.arraycopy(input, 0, str, 0, input.length); } else { str = input; }
}
Exit:
Before: length 8 >< >1< >< >null< >2< > < >3< >< After: length 3 >1< >2< >3<
source share