数组小案例-移除另一个数组的所有元素

题目:
来源 某论坛
我要删除一个数组中的元素,该怎么做。比如:
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);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值