Bei dem folgenden Java-Code gefunden:kein geeignetes Verfahren zum addAll
public void addStuff(Collection<? extends Object> aCollection) {
List<? extends Object> myList = new LinkedList<>(aCollection);
myList.addAll(aCollection);
}
Der Konstruktoraufruf kompiliert, aber der addAll() -Aufruf nicht zu kompilieren. Ich erhalte den folgenden Fehler:
no suitable method found for addAll(Collection<CAP#1>)
method Collection.addAll(Collection<? extends CAP#2>) is not applicable
(argument mismatch; Collection<CAP#1> cannot be converted to Collection<? extends CAP#2>)
method List.addAll(Collection<? extends CAP#2>) is not applicable
(argument mismatch; Collection<CAP#1> cannot be converted to Collection<? extends CAP#2>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends Object from capture of ? extends Object
CAP#2 extends Object from capture of ? extends Object
Auch wenn sie die gleiche Signatur haben, und man nennt das andere:
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
Was hier los?