题目:
来源 某论坛
我要删除一个数组中的元素,该怎么做。比如:
String a[] = {“1”,“2”,“3”,“4”,“5”,“6”,""};
String b[] = {“3”,“4”,“5”};
- 我要将a中包含b的元素删除。就是把3,5,1,""分别删除。然后转换成数组
- 转换成数组要去掉 [ ]
- 把应该怎么做,求大神指点。拜谢
public class ArrayDemo1 {
public static void main(String[] args) {
String a[] = {"1","2","3","4","5","6","",null};
String b[] = {"3","4","5"};
1. 首先转换为 List格式
注: 不要 List<String> as = Arrays.asList(a)
因为arrays是没有removeAll方法所以会报错
List<String> as = new ArrayList(Arrays.asList(a));
List<String> bs = new ArrayList(Arrays.asList(b));
2. 调用移除removeAll去除
as.removeAll(bs);
// as.add(null);
// as.add("");
3. 去除数组中的所有空字符串以及null
while ( as.remove(null) && as.remove("")){};
4. 正则去掉所有[]
String string = as.toString();
string = string.replaceAll("[\\[\\]]","");
System.out.println(string);
}
}