简体   繁体   English

当使用 ArrayList(Collection<? extends E> c) 对于复制构造函数,我收到两个错误

[英]When using ArrayList(Collection<? extends E> c) for a copy constructor I get two errors

public ListArrayListBased(ListArrayListBased < E > var) 

{

items = new ArrayList < E> (ArrayList(ListArrayListBased 
        < ? extends E > var));

}

Using this line of code gives me two errors.使用这行代码给了我两个错误。

  1. Wildcard is not allowed at this location.此位置不允许使用通配符。
  2. Syntax Error on token "ListArrayListBased", :: expected after token.标记“ListArrayListBased”上的语法错误,:: 预期在标记之后。

I was able to fix the second one by putting :: after ListArrayListBased but I have no clue what that did or how it worked.我能够通过在 ListArrayListBased 之后放置 :: 来修复第二个,但我不知道它做了什么或它是如何工作的。 I am looking more for an explanation on the errors then on the solutions to the problem.我正在寻找更多关于错误的解释,然后是问题的解决方案。 Thanks!谢谢!

This is a copy constructor, and you're trying to assign the items member variable, which is presumably the list inside your class.这是一个复制构造函数,您正在尝试分配items成员变量,这可能是您的类中的列表。

So, copy the items list from var :因此,从var复制items列表:

items = new ArrayList<>(var.items);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM