java 集合addall
集合类的addAll()方法 (Collections Class addAll() method)
addAll() Method is available in java.lang package.
addAll()方法在java.lang包中可用。
addAll() Method is used to put all the given elements(ele) to the given collection (co).
addAll()方法用于将所有给定的element( ele )放入给定的集合( co )。
addAll() Method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
addAll()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
addAll() Method may throw an exception at the time of add the elements(ele) to the given Collection(co).
在将elements( ele )添加到给定Collection( co )时, addAll()方法可能会引发异常。
- UnsupportedOperationException: This exception may throw when collection unsupport add() method.UnsupportedOperationException :集合不支持add()方法时,可能引发此异常。
- NullPointerException: This exception may throw when elements (ele) may have at least one null & the given collection unsupport null.
- NullPointerException :当元素( ele )可能至少具有一个null且给定的集合不支持null时,可能引发此异常。
- IllegalArgumentException: This exception may throw when the given element (ele) is not valid.
- IllegalArgumentException :如果给定元素( ele )无效,则可能引发此异常。
Syntax:
句法:
public static boolean addAll(Collection co, Type.. ele);
Parameter(s):
参数:
Collection co – represents the container of "Collection" type.
集合co –表示“集合”类型的容器。
Type.. ele – represents the elements to add into given collection co.
Type .. ele –表示要添加到给定集合co中的元素。
Return value:
返回值:
The return type of the method is Boolean, it returns true when the given set of elements(ele) to be added into collection successfully otherwise it returns false.
该方法的返回类型为Boolean ,如果要成功将给定的元素集(ele)添加到集合中,则返回true,否则返回false。
Example:
例:
// Java Program is to demonstrate the example
// of boolean addAll(Collection co, Type.. ele) of Collections class
import java.util.*;
public class AddAll {
public static void main(String args[]) {
// Create a linked list object
List link_list = new LinkedList();
// By using add() method is to add the
// given elements in linked list
link_list.add(10);
link_list.add(20);
link_list.add(30);
link_list.add(40);
link_list.add(50);
//Display Linked List
System.out.println("link_list: " + link_list);
// By using addAll() method is to add all the
// elements in the given collection linked list
boolean status = Collections.addAll(link_list, 60, 70, 80, 90);
System.out.println();
System.out.println("Collections.addAll(link_list, 60,70,80,90) :");
// Display Linked List
System.out.println("link_list: " + link_list);
}
}
Output
输出量
link_list: [10, 20, 30, 40, 50]Collections.addAll(link_list, 60,70,80,90) :
link_list: [10, 20, 30, 40, 50, 60, 70, 80, 90]
翻译自: https://www.includehelp.com/java/collections-addall-method-with-example.aspx
java 集合addall