📜  Java中的 AbstractList addAll() 方法及示例

📅  最后修改于: 2022-05-13 01:54:38.521000             🧑  作者: Mango

Java中的 AbstractList addAll() 方法及示例

Java.util.AbstractList类的addAll()方法用于将指定集合中的所有元素插入到此列表中指定位置。

  • 这会将当前位于该位置的元素(如果有)和任何后续元素向右移动(增加它们的索引)。
  • 新元素将按照指定集合的迭代器返回的顺序出现在此列表中。
  • 如果在操作正在进行时修改了指定的集合,则此操作的行为是未定义的。

此实现在指定集合上获取一个迭代器并对其进行迭代,使用 add(int, E) 将从迭代器获得的元素插入到此列表中的适当位置,一次一个。许多实现将覆盖此方法以提高效率。

句法:

public boolean addAll(int index, Collection c)

参数:此方法将以下参数作为参数。

  • index - 插入指定集合中第一个元素的索引
  • c-包含要添加到此列表的元素的集合
  • 返回值:如果此列表因调用而更改,则此方法返回true

  • 异常:此方法引发以下异常。
    • NullPointerException –如果指定的集合包含一个或多个空元素并且此列表不允许空元素,或者指定的集合为空
    • IndexOutOfBoundsException –如果索引超出范围(索引大小())。

下面是说明addAll()方法的示例。

示例 1:

// Java program to demonstrate
// addAll() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("Original ArrayListlist : "
                               + arrlist1);
  
            // Creating another object of AbstractList
            AbstractList
                arrlist2 = new ArrayList();
  
            // Populating arrlist2
            arrlist2.add("X");
            arrlist2.add("Y");
            arrlist2.add("Z");
  
            // print arrlist2
            System.out.println("ArrayList elements "
                               + "to be added : "
                               + arrlist2);
  
            // adding the specified element
            // starting from index 2
            // using addAll() method
            boolean value = arrlist1.addAll(2, arrlist2);
  
            // print the value
            System.out.println("Operation successful : "
                               + value);
  
            // print the new arrlist
            System.out.println("New ArrayList : "
                               + arrlist1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Original ArrayListlist : [A, B, C, D, E]
ArrayList elements to be added : [X, Y, Z]
Operation successful : true
New ArrayList : [A, B, X, Y, Z, C, D, E]

示例 2:对于NullPointerException

// Java program to demonstrate
// addAll() method
// for NullPointerException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("Original ArrayListlist : "
                               + arrlist1);
  
            // Creating another object of AbstractList
            AbstractList arrlist2 = null;
  
            // printing the arrlist2
            System.out.println("ArrayList to be added : "
                               + arrlist2);
  
            // adding the specified element
            // starting from index 2
            // using addAll() method
            System.out.println("\nTrying to get"
                               + " the null collection");
            boolean value = arrlist1.addAll(2, arrlist2);
  
            // print the value
            System.out.println("operation successful : "
                               + value);
  
            // print the new arrlist
            System.out.println("New ArrayList : "
                               + arrlist1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Original ArrayListlist : [A, B, C, D, E]
ArrayList to be added : null

Trying to get the null collection
Exception thrown : java.lang.NullPointerException

示例 3:对于IndexOutOfBoundsException

// Java program to demonstrate
// addAll() method
// for IndexOutOfBoundsException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("Original ArrayListlist : "
                               + arrlist1);
  
            // Creating another object of AbstractList
            AbstractList
                arrlist2 = new ArrayList();
  
            // Populating arrlist2
            arrlist2.add("X");
            arrlist2.add("Y");
            arrlist2.add("Z");
  
            // print arrlist2
            System.out.println("ArrayList elements to be added : "
                               + arrlist2);
  
            // adding the specified element
            // starting from index 2
            // using addAll() method
            System.out.println("\nTrying to put the out"
                               + " of range index ");
            boolean value = arrlist1.addAll(-1, arrlist2);
  
            // print the value
            System.out.println("operation succecfull : "
                               + value);
  
            // print the new arrlist
            System.out.println("New ArrayList : " + arrlist1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Original ArrayListlist : [A, B, C, D, E]
ArrayList elements to be added : [X, Y, Z]

Trying to put the out of range index 
Exception thrown : java.lang.IndexOutOfBoundsException: Index: -1, Size: 5