如何从 ArrayList 中删除空白项.无需删除索引 [英] how to remove blank items from ArrayList.Without removing index wise

查看:18
本文介绍了如何从 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 [, 姓名, , , asd]欲望 O/p [name,asd]

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

推荐答案

您可以使用 removeAll(Collection c) :

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天全站免登陆