我不确定我是否正确使用了泛型,但基本上我创建了一个Arraylist<? extends ModuleInfo> moduleList
andModuleInfo m
对象,并尝试调用moduleList.add(m)
. 但是它不会编译,我收到一条对我来说似乎有点神秘的错误消息。错误消息和代码如下。其他人知道出了什么问题吗?
void load() {
ArrayList<? extends ModuleInfo> moduleList = new ArrayList();
Iterator<? extends ModuleInfo> iter_m;
ModuleInfo m;
//get modules that depend on this module
//retrieve list of all modules and iterate trough each one
iter_m = Lookup.getDefault().lookupAll(ModuleInfo.class).iterator();
while(iter_m.hasNext()) {
m = iter_m.next();
//loop through modules dependencies and check for a dependency on this module
for(Dependency d : m.getDependencies()) {
//if found, the module to the list
if(d.getName().equals(GmailAuthManager.class.getPackage().getName())) {
moduleList.add(m);
break;
}
}
}
}
错误如下:
error: no suitable method found for add(ModuleInfo)
moduleList.add(m);
method ArrayList.add(int,CAP#1) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(CAP#1) is not applicable
(actual argument ModuleInfo cannot be converted to CAP#1 by method invocation conversion)
where CAP#1 is a fresh type-variable:
CAP#1 extends ModuleInfo from capture of ? extends ModuleInfo