如何在不删除索引的情况下从ArrayList中删除空白项 [英] how to remove blank items from ArrayList.Without removing index wise

查看:98
本文介绍了如何在不删除索引的情况下从ArrayList中删除空白项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ArrayListTest {

    public static void main(String[] args) {
        ArrayList al=new ArrayList();
        al.add("");
        al.add("name");
        al.add("");
        al.add("");
        al.add(4, "asd");
        System.out.println(al);
    }
}

o/p [,name,,,asd] 渴望O/p [姓名,asd]

o/p [, name, , , asd] desire O/p [name,asd]

推荐答案

您可以使用

You can use removeAll(Collection<?> c) :

删除此集合中也包含在其中的所有元素 指定的集合

Removes all of this collection's elements that are also contained in the specified collection

al.removeAll(Arrays.asList(null,""));

这将删除List中所有null或等于""的元素.

This will remove all elements that are null or equals to "" in your List.

输出:

[name, asd]

这篇关于如何在不删除索引的情况下从ArrayList中删除空白项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆