博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 集合addall_Java集合的addAll()方法和示例
阅读量:2529 次
发布时间:2019-05-11

本文共 2793 字,大约阅读时间需要 9 分钟。

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 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 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]

翻译自:

java 集合addall

转载地址:http://gatzd.baihongyu.com/

你可能感兴趣的文章
hibernate could not resolve property
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
骨牌覆盖问题
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
Python 函数参数 传引用还是传值
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>