Java中的AbstractSequentialList removeAll()方法示例

Java中的AbstractSequentialList removeAll()方法示例

java.util.AbstractSequentialList类的 removeAll() 方法用于从列表中删除包含在指定集合中的所有元素。

语法:

public boolean removeAll(Collection c)

参数: 此方法将包含要从此列表中删除的元素的集合c作为参数。

返回值: 如果此列表因调用而更改,则此方法返回true。

异常: 如果此列表包含null元素并且指定的集合不允许null元素(可选),或者指定的集合为null,则此方法会抛出NullPointerException。

以下是说明removeAll()方法的示例。

示例1:

// 演示
// Integer值的removeAll()方法的Java程序
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
  
            // AbstractSequentialList<Integer>对象的创建
            AbstractSequentialList<Integer>
                arrlist1 = new LinkedList<Integer>();
  
            // 填充arrlist1
            arrlist1.add(1);
            arrlist1.add(2);
            arrlist1.add(3);
            arrlist1.add(4);
            arrlist1.add(5);
  
            // 打印arrlist1
            System.out.println("操作removeAll()之前的AbstractSequentialList:"
                               + arrlist1);
  
            // AbstractSequentialList<Integer>的另一个对象的创建
            AbstractSequentialList<Integer>
                arrlist2 = new LinkedList<Integer>();
            arrlist2.add(1);
            arrlist2.add(2);
            arrlist2.add(3);
  
            // 打印arrlist2
            System.out.println("待删除的Collection Elements:"
                               + arrlist2);
  
            // 使用removeAll()方法从arrlist中删除在arrlist2中指定的元素
            arrlist1.removeAll(arrlist2);
  
            // 打印arrlist1
            System.out.println("操作removeAll()之后的AbstractSequentialList:"
                               + arrlist1);
        }
  
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
操作removeAll()之前的AbstractSequentialList:[1, 2, 3, 4, 5]
待删除的Collection Elements:[1, 2, 3]
操作removeAll()之后的AbstractSequentialList:[4, 5]

示例2: 用于NullPointerException

// Java程序演示
//用于整数值的removeAll()方法

import java.util.*;

public class GFG1 {
    public static void main(String[] argv) throws Exception {

        try{

            //创建AbstractSequentialList对象
            AbstractSequentialList 
                arrlist1=new LinkedList();

            //填充arrlist1
            arrlist1.add(1);
            arrlist1.add(2);
            arrlist1.add(3);
            arrlist1.add(4);
            arrlist1.add(5);

            //打印arrlist1
            System.out.println("AbstractSequentialList before "
                                    + "removeAll() operation : "
                                    + arrlist1);

            //创建另一个AbstractSequentialList对象
            AbstractSequentialList
                arrlist2=null;

            //打印arrlist2
            System.out.println("Collection Elements"
                                    + " to be removed : "
                                    + arrlist2);

            System.out.println("\nTrying to pass "
                                    + "null as a specified element\n");

            //使用removeAll()方法从arrlist中删除在arrlist2中指定的元素
            arrlist1.removeAll(arrlist2);

            //打印arrlist1
            System.out.println("AbstractSequentialList after "
                                    + "removeAll() operation : "
                                    + arrlist1);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}
AbstractSequentialList before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : null

Trying to pass null as a specified element

java.lang.NullPointerException

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程