从java中的String数组中删除Null值

如何从java中的String数组中删除null值?

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

我需要“firstArray”没有像这样的null(空)值

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

如果你想避免使用fencepost错误并避免移动和删除数组中的项目,这里有一个使用List的有点冗长的解决方案:

 import java.util.ArrayList; import java.util.List; public class RemoveNullValue { public static void main( String args[] ) { String[] firstArray = {"test1", "", "test2", "test4", "", null}; List list = new ArrayList(); for(String s : firstArray) { if(s != null && s.length() > 0) { list.add(s); } } firstArray = list.toArray(new String[list.size()]); } } 

添加null以显示空String实例( "" )和null之间的差异。

由于这个答案大约是4.5岁,我正在添加一个Java 8示例:

 import java.util.Arrays; import java.util.stream.Collectors; public class RemoveNullValue { public static void main( String args[] ) { String[] firstArray = {"test1", "", "test2", "test4", "", null}; firstArray = Arrays.stream(firstArray) .filter(s -> (s != null && s.length() > 0)) .toArray(String[]::new); } } 

如果您确实想要从数组添加/删除项目,我可以建议使用List吗?

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

然后,如果你真的需要把它放回一个数组:

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

使用谷歌的番石榴库

 String[] firstArray = {"test1","","test2","test4","",null}; Iterable st=Iterables.filter(Arrays.asList(firstArray),new Predicate() { @Override public boolean apply(String arg0) { if(arg0==null) //avoid null strings return false; if(arg0.length()==0) //avoid empty strings return false; return true; // else true } }); 

这是我用来从不使用数组列表的数组中删除空值的代码。

 String[] array = {"abc", "def", null, "g", null}; // Your array String[] refinedArray = new String[array.length]; // A temporary placeholder array int count = -1; for(String s : array) { if(s != null) { // Skips over null values. Add "|| "".equals(s)" if you want to exclude empty strings refinedArray[++count] = s; // Increments count and sets a value in the refined array } } // Returns an array with the same data but refits it to a new length array = Arrays.copyOf(refinedArray, count + 1); 

似乎没有人提到过使用nonNull方法,它也可以与Java 8中的 streams一起用于删除null(但不是空),如下所示:

 String[] origArray = {"Apple", "", "Cat", "Dog", "", null}; String[] cleanedArray = Arrays.stream(firstArray).filter(Objects::nonNull).toArray(String[]::new); System.out.println(Arrays.toString(origArray)); System.out.println(Arrays.toString(cleanedArray)); 

输出是:

[Apple ,, Cat,Dog ,, null]

[苹果,猫,狗,]

如果我们想要合并为空,那么我们可以定义一个实用工具方法(在类Utils (比如说)中):

 public static boolean isEmpty(String string) { return (string != null && !string.isEmpty()); } 

然后使用它来过滤项目:

 Arrays.stream(firstArray).filter(Utils::isEmpty).toArray(String[]::new); 

我相信Apache common还提供了一个实用方法StringUtils.isNotEmpty ,它也可以使用。

这些是零长度字符串,而不是null。 但是如果你想删除它们:

 firstArray[0] refers to the first element firstArray[1] refers to the second element 

你可以将第二个移到第一个:

 firstArray[0] = firstArray[1] 

如果你要为元素[1,2],然后[2,3]等做这个,你最终会将数组的全部内容移到左边,从而消除元素0.你能看出它会如何应用吗?