Java 方法Collections.copy的编译器错误

Java 方法Collections.copy的编译器错误,java,list,collections,java-8,Java,List,Collections,Java 8,我写这个程序是为了把一个列表复制到另一个列表,但是我得到了一个错误。在编译其他程序时,我也遇到了这个Collections.sort()和Collections.copy()错误。有人能帮我解决这个问题吗 import java.util.*; import java.util.ArrayList; import java.util.Collection; class CopyList { public static void main(String[] args) {

我写这个程序是为了把一个列表复制到另一个列表,但是我得到了一个错误。在编译其他程序时,我也遇到了这个
Collections.sort()
Collections.copy()
错误。有人能帮我解决这个问题吗

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;

class CopyList {

    public static void main(String[] args) {

        Collection<String> srcstr = new ArrayList<String>();

        srcstr.add("New York");
        srcstr.add("Atlanta");
        srcstr.add("Dallas");
        srcstr.add("Madison");
        System.out.println("Number of elements: " + srcstr.size());
        srcstr.forEach(s->System.out.println(s));
        Collection<String> deststr = new ArrayList<String>();
        deststr.add("Delhi");
        deststr.add("Mumbai");
        Collections.copy(srcstr,deststr);
        deststr.forEach(s->System.out.println(s));
    }

}
import java.util.*;
导入java.util.ArrayList;
导入java.util.Collection;
类复制列表{
公共静态void main(字符串[]args){
集合srcstr=newarraylist();
srcstr.add(“纽约”);
srcstr.add(“亚特兰大”);
srcstr.add(“达拉斯”);
srcstr.add(“麦迪逊”);
System.out.println(“元素数:+srcstr.size());
srcstr.forEach->System.out.println;
Collection deststr=new ArrayList();
目的地添加(“德里”);
目的地添加(“孟买”);
Collections.copy(srcstr、deststr);
deststr.forEach->System.out.println;
}
}
我得到的错误是:

CopyList.java:17: error: method copy in class Collections cannot be applied to given types;
                Collections.copy(srcstr,deststr);

                           ^
  required: List<? super T>,List<? extends T>

  found: Collection<String>,Collection<String>

  reason: cannot infer type-variable(s) T

    (argument mismatch; Collection<String> cannot be converted to List<? super T>)

  where T is a type-variable:

    T extends Object declared in method <T>copy(List<? super T>,List<? extends T>)
CopyList.java:17:错误:类集合中的方法copy无法应用于给定类型;
Collections.copy(srcstr、deststr);
^

必需:列表使用arraylist更改收藏列表

List<String> srcstr = new ArrayList<String>();
srcstr.add("New York");
srcstr.add("Atlanta");
srcstr.add("Dallas");
srcstr.add("Madison");

List<String> deststr = new ArrayList<String>();
deststr.add("Delhi");
deststr.add("Mumbai");

Collections.copy(srcstr, deststr);
List srcstr=new ArrayList();
srcstr.add(“纽约”);
srcstr.add(“亚特兰大”);
srcstr.add(“达拉斯”);
srcstr.add(“麦迪逊”);
List deststr=new ArrayList();
目的地添加(“德里”);
目的地添加(“孟买”);
Collections.copy(srcstr、deststr);

使用arraylist更改收藏列表

List<String> srcstr = new ArrayList<String>();
srcstr.add("New York");
srcstr.add("Atlanta");
srcstr.add("Dallas");
srcstr.add("Madison");

List<String> deststr = new ArrayList<String>();
deststr.add("Delhi");
deststr.add("Mumbai");

Collections.copy(srcstr, deststr);
List srcstr=new ArrayList();
srcstr.add(“纽约”);
srcstr.add(“亚特兰大”);
srcstr.add(“达拉斯”);
srcstr.add(“麦迪逊”);
List deststr=new ArrayList();
目的地添加(“德里”);
目的地添加(“孟买”);
Collections.copy(srcstr、deststr);

错误消息说明了一切,您的变量都是
集合
类型,并且它们需要是
列表
类型,只需执行
列表srcstr=…
而不是
集合srcstr=…
,如果您打算从
srcstr
复制到
deststr
,正如变量名称所示,您需要仔细查看。错误消息说明了这一切,您的变量都是
Collection
类型,并且它们必须是
List
类型,如果您打算从
srcstr
复制到
deststr
,只需执行
List srcstr=…
而不是
Collection srcstr=…
,即可,正如变量名称所示,您需要仔细查看。