Java generic不适用于参数列表<;对象>;
我正在玩Java中的泛型。但我现在确实有问题。简而言之,这段代码应该转换一个列表中的一个对象,而不是将其放入另一个列表中。(从T到U) 我的代码是这样的Java generic不适用于参数列表<;对象>;,java,java-8,Java,Java 8,我正在玩Java中的泛型。但我现在确实有问题。简而言之,这段代码应该转换一个列表中的一个对象,而不是将其放入另一个列表中。(从T到U) 我的代码是这样的 public class ListConvertor<T, U> { private final Convertor<T, ? extends U> convertor; public ListConvertor( Convertor<T, ? extends U> convertor )
public class ListConvertor<T, U> {
private final Convertor<T, ? extends U> convertor;
public ListConvertor( Convertor<T, ? extends U> convertor ) {
this.convertor = convertor;
}
public void convertList( List<T> from, List<U> to ) {
if ( from == null )
return;
for ( T ch : from ) {
to.add( convertor.convert( ch ) );
}
}
}
public interface Convertor<T, V> {
V convert( T dataModelObject ) throws JEDXException;
}
但我也希望将它与List
on-out参数一起使用。所以看起来是这样的:
List<Object> outObjectList = new ArrayList<Object>();
new ListConvertor<>(new IntListConvertor()).convertList(in.getIntLists(), outObjectList );
List outObjectList=new ArrayList();
新的ListConvertor(新的IntListConvertor()).convertList(in.getIntLists(),outObjectList);
但这样使用时,我会犯错误:
The method convertList(List<IntCharacteristic>, List<IntCharacteristicType>) in the type ListConvertor<IntCharacteristic,IntCharacteristicType> is not applicable for the arguments (List<IntCharacteristic>, List<Object>)
类型ListConvertor中的方法convertList(List,List)不适用于参数(List,List)
您应该将方法签名从
public-void-convertList(列表从,列表到)
到
public void convertList(List from,List您应该将方法签名从
public-void-convertList(列表从,列表到)
到
public void convertList(列表来源,列表
List<Object> outObjectList = new ArrayList<Object>();
new ListConvertor<>(new IntListConvertor()).convertList(in.getIntLists(), outObjectList );
The method convertList(List<IntCharacteristic>, List<IntCharacteristicType>) in the type ListConvertor<IntCharacteristic,IntCharacteristicType> is not applicable for the arguments (List<IntCharacteristic>, List<Object>)