日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Collections copy()方法与示例

發布時間:2025/3/11 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Collections copy()方法与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

集合類的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 objectList < Integer > src_l = new LinkedList < Integer > ();List < Integer > dest_l = new LinkedList < Integer > ();// By using add() method is to add// few elements in src_lsrc_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_ldest_l.add(60);dest_l.add(70);dest_l.add(80);dest_l.add(90);// Display LinkedListSystem.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_lCollections.copy(dest_l, src_l);System.out.println();// Display Copied LinkedListSystem.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]

翻譯自: https://www.includehelp.com/java/collections-copy-method-with-example.aspx

總結

以上是生活随笔為你收集整理的Java Collections copy()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。