In Java, remove empty elements from a list of Strings

前端 未结 11 2390
余生分开走
余生分开走 2020-12-01 06:05

In Java, I have an ArrayList of Strings like:

[,Hi, ,How,are,you]

I want to remove the null and empty elements, how to change it so it is l

11条回答
  •  离开以前
    2020-12-01 06:30

       private List cleanInputs(String[] inputArray) {
            List result = new ArrayList(inputArray.length);
            for (String input : inputArray) {
                if (input != null) {
                    String str = input.trim();
                    if (!str.isEmpty()) {
                        result.add(str);
                    }
                }
            }
            return result;
        }
    

提交回复
热议问题