博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Collections copy()方法与示例
阅读量:2533 次
发布时间:2019-05-11

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

集合类的copy()方法 (Collections Class copy() method)

  • copy() method is available in java.util package.

    copy()方法在java.util包中可用。

  • copy() method is used to copy all the elements from List the src_list (source list) and place all the copied elements into List dst_list (destination list).

    copy()方法用于从src_list列表(源列表)中复制所有元素,并将所有复制的元素放入dst_list列表(目标列表)中。

  • copy() method is a static method, so 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.

    copy()方法是一个静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。

  • copy() method may throw an exception at the time of copying elements from one list to another.

    将元素从一个列表复制到另一个列表时, copy()方法可能会引发异常。

    • IndexOutOfBoundsException: This exception may throw when the given parameter dst_list size is lesser than the src_list.
    • IndexOutOfBoundsException :当给定参数dst_list的大小小于src_list时,可能引发此异常。
    • UnsupportedOperationException: This exception may throw when the given parameter dst_list un-support set operation.
    • UnsupportedOperationException :如果给定参数dst_list不支持set操作,则可能引发此异常。

Syntax:

句法:

public static void copy(List dst_list, List src_list);

Parameter(s):

参数:

  • List dst_list – represents the destination list.

    列表dst_list –表示目标列表。

  • List src_list – represents the source list.

    列表src_list –表示源列表。

Return value:

返回值:

The return type of this method is void, it returns nothing.

此方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program is to demonstrate the example of// void copy() method of Collections import java.util.*;public class Copy {
public static void main(String args[]) {
// Instantiate two LinkedList object List < Integer > src_l = new LinkedList < Integer > (); List < Integer > dest_l = new LinkedList < Integer > (); // By using add() method is to add // few elements in src_l src_l.add(10); src_l.add(20); src_l.add(30); src_l.add(40); // By using add() method is to add // few elements in dest_l dest_l.add(60); dest_l.add(70); dest_l.add(80); dest_l.add(90); // Display LinkedList System.out.println("src_l: " + src_l); System.out.println("dest_l: " + dest_l); // By using copy() method is to // copy the elements of src_l into a dest_l Collections.copy(dest_l, src_l); System.out.println(); // Display Copied LinkedList System.out.println("Collections.copy(dest_l, src_l): " + dest_l); }}

Output

输出量

src_l: [10, 20, 30, 40]dest_l: [60, 70, 80, 90]Collections.copy(dest_l, src_l): [10, 20, 30, 40]

翻译自:

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

你可能感兴趣的文章
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>