提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
删除字符串数组中的空元素
//package com.nowjava; import java.util.ArrayList;/** from N o w J a v a . c o m**/ import java.util.List; public class Main { public static void main(String[] argv) throws Exception { String[] origArray = new String[] { "1", "abc", "level", null, "nowjava.com", "asdf 123" }; System.out.println(java.util.Arrays .toString(removeEmpty(origArray))); } public static String[] removeEmpty(String[] origArray) { List<String> newArr = new ArrayList<String>();/**来自 nowjava**/ for (String el : origArray) { if (hasText(el)) { newArr.add(el); } } return newArr.toArray(new String[newArr.size()]); }