java.util.AbstractList 類的 addAll() 方法用於將指定集合中的所有元素插入到此列表中的指定位置。
- 這會將當前位於該位置的元素(如果有)和任何後續元素向右移動(增加它們的索引)。
- 新元素將按照指定集合的迭代器返回的順序出現在此列表中。
- 如果在操作正在進行時修改了指定的集合,則此操作的行為是未定義的。
此實現在指定集合上獲取一個迭代器並對其進行迭代,使用 add(int, E) 將從迭代器獲得的元素插入到此列表中的適當位置,一次一個。許多實現將覆蓋此方法以提高效率。
用法:
public boolean addAll(int index, Collection c)
參數:此方法將以下參數作為參數。
- index-插入指定集合中第一個元素的索引
- c-包含要添加到此列表的元素的集合
返回值:如果此列表因調用而更改,則此方法返回 true。
- Exception:此方法引發以下異常。
- NullPointerException -如果指定的集合包含一個或多個空元素並且此列表不允許空元素,或者指定的集合為空
- IndexOutOfBoundsException -如果索引超出範圍(索引 size())。
以下是說明 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<String>
AbstractList<String>
arrlist1 = new ArrayList<String>();
// 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<String>
AbstractList<String>
arrlist2 = new ArrayList<String>();
// 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<String>
AbstractList<String>
arrlist1 = new ArrayList<String>();
// 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<String>
AbstractList<String> 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<String>
AbstractList<String>
arrlist1 = new ArrayList<String>();
// 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<String>
AbstractList<String>
arrlist2 = new ArrayList<String>();
// 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
相關用法
- Java AbstractList clear()用法及代碼示例
- Java AbstractList equals()用法及代碼示例
- Java AbstractList get()用法及代碼示例
- Java AbstractList hashCode()用法及代碼示例
- Java AbstractList iterator()用法及代碼示例
- Java AbstractList lastIndexOf()用法及代碼示例
- Java AbstractList indexOf()用法及代碼示例
- Java AbstractList listIterator()用法及代碼示例
- Java AbstractList remove()用法及代碼示例
- Java AbstractList set()用法及代碼示例
- Java AbstractList add(E ele)用法及代碼示例
- Java AbstractList subList()用法及代碼示例
- Java AbstractList add(int index, E element)用法及代碼示例
- Java Collections addAll()用法及代碼示例
- Java AbstractQueue addAll()用法及代碼示例
- Java AbstractCollection addAll()用法及代碼示例
- Java AbstractSequentialList addAll()用法及代碼示例
- Java Collection addAll()用法及代碼示例
- Java ConcurrentLinkedDeque addAll()用法及代碼示例
- Java Set addAll()用法及代碼示例
- Java List addAll()用法及代碼示例
- Java CopyOnWriteArraySet addAll()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
- Java LinkedBlockingDeque addAll()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 AbstractList addAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。