Removing empty element from Array(Java)

后端 未结 5 812
遇见更好的自我
遇见更好的自我 2020-12-10 09:12

I\'m trying to remove the empty element from the array by copying the existing element to a new array. However, initialization of the new array is causing my return value to

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 10:13

    I'm actually not sure what you want to achieve but if you want to remove an empty String out of your array you can do it with streams and filters in java 8 like this:

    String[] objects = Arrays.stream(new String[]{"This","", "will", "", "", "work"}).filter(x -> !x.isEmpty()).toArray(String[]::new);
    

提交回复
热议问题