Java Collections copy()

copy() 方法将一个List中的所有元素复制到另一个List中。

1 语法

public static <T> void copy(List<? super T> dest, List<? extends T> src)

2 参数

dest:需要复制的List集合。

src:源目标的List集合。

3 返回值

4 Collections copy()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.copy的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create lists for source and destination
        List<String> srclist = new ArrayList<String>(5);
        List<String> destlist = new ArrayList<String>(10);
        //Populate two source and destination lists
        srclist.add("Yiidian");
        srclist.add("is a website");
        srclist.add("learning IT");
        destlist.add("Sm1234");
        destlist.add("is old than");
        destlist.add("Yiidian");
        // copy into destination list
        Collections.copy(destlist, srclist);
        System.out.println("Elements of source list: "+srclist);
        System.out.println("Elements of destination list: "+destlist);
    }
}

输出结果为:

Elements of source list: [Yiidian, is a website, learning IT]
Elements of destination list: [Yiidian, is a website, learning IT]

5 Collections copy()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.copy的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create lists for source and destination
        List<Integer> source = Arrays.asList(1,2,3,4);
        List<Integer> dest = Arrays.asList(5,6,7,8,9,10);
        Collections.copy(dest, source);
        //Print the List
        for(Integer i : dest) {
            System.out.print(i +" ");
        }
    }
}

输出结果为:

1 2 3 4 9 10 

6 Collections copy()示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.copy的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create lists for source and destination
        List<Integer> source = Arrays.asList(1,2,3,4);
        List<Integer> dest = Arrays.asList(5,6,7);
        Collections.copy(dest, source);
        //Print the List
        System.out.println(dest);
    }
}

输出结果为:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest
	at java.util.Collections.copy(Collections.java:556)
	at com.yiidian.Demo.main(Demo.java:17)

7 Collections copy()示例4

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.copy的例子
 */
import java.util.*;

public class Demo {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        //Create lists
        ArrayList source = new ArrayList();
        source.add(50);
        source.add(10);
        source.add(20);
        System.out.println("Elements of Source List: " + source);
        ArrayList dest = new ArrayList();
        dest.add("one");
        dest.add("two");
        dest.add("three");
        dest.add("four");
        dest.add("five");
        System.out.println("Elements of Destination List: " + dest);
        Collections.copy(dest, source);
        System.out.println("Elements of Destination List after copying Source List: " + dest);
    }
}

输出结果为:

Elements of Source List: [50, 10, 20]
Elements of Destination List: [one, two, three, four, five]
Elements of Destination List after copying Source List: [50, 10, 20, four, five]

 

热门文章

优秀文章