Remove Null Value from String array in java

前端 未结 8 668
囚心锁ツ
囚心锁ツ 2020-11-29 08:16

How to remove null value from String array in java?

String[] firstArray = {\"test1\",\"\",\"test2\",\"test4\",\"\"};

I need the \"firstArra

8条回答
  •  感动是毒
    2020-11-29 08:47

    If you actually want to add/remove items from an array, may I suggest a List instead?

    String[] firstArray = {"test1","","test2","test4",""};
    ArrayList list = new ArrayList();
    for (String s : firstArray)
        if (!s.equals(""))
            list.add(s);
    

    Then, if you really need to put that back into an array:

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

提交回复
热议问题