Java ArrayList.removeAll()方法

在Java中,使用 ArrayList.removeAll() 清空数组列表示例:

Java的 removeAll()方法将删除数组列表的所有元素。

import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));

        System.out.println(list);

        list.removeAll(list);   //remove all elements from self

        System.out.println(list);
    }
}

程序输出。

[a, b, c, d, e]
[]
日期:2020-09-17 00:09:32 来源:oir作者:oir