java 集合addall_Java集合的addAll()方法和示例
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()方法是一個(gè)靜態(tài)方法,可以使用類名進(jìn)行訪問(wèn),如果嘗試使用類對(duì)象訪問(wèn)該方法,則不會(huì)收到錯(cuò)誤。
addAll() Method may throw an exception at the time of add the elements(ele) to the given Collection(co).
在將elements( ele )添加到給定Collection( co )時(shí), addAll()方法可能會(huì)引發(fā)異常。
- UnsupportedOperationException: This exception may throw when collection unsupport add() method.UnsupportedOperationException :集合不支持add()方法時(shí),可能引發(fā)此異常。
- NullPointerException: This exception may throw when elements (ele) may have at least one null & the given collection unsupport null.
- NullPointerException :當(dāng)元素( ele )可能至少具有一個(gè)null且給定的集合不支持null時(shí),可能引發(fā)此異常。
- IllegalArgumentException: This exception may throw when the given element (ele) is not valid.
- IllegalArgumentException :如果給定元素( ele )無(wú)效,則可能引發(fā)此異常。
Syntax:
句法:
public static boolean addAll(Collection co, Type.. ele);Parameter(s):
參數(shù):
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 classimport 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 listlink_list.add(10);link_list.add(20);link_list.add(30);link_list.add(40);link_list.add(50);//Display Linked ListSystem.out.println("link_list: " + link_list);// By using addAll() method is to add all the// elements in the given collection linked listboolean 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 ListSystem.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
總結(jié)
以上是生活随笔為你收集整理的java 集合addall_Java集合的addAll()方法和示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: stl中copy()函数_std ::
- 下一篇: 茱莉亚分形_茱莉亚的NaN Consta