无法将 ModuleInfo 对象添加到 ArrayList<?扩展模块信息> [英] Can&#39;t add a ModuleInfo object to ArrayList&lt;? extends ModuleInfo&gt;

查看:14
本文介绍了无法将 ModuleInfo 对象添加到 ArrayList<?扩展模块信息>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确使用泛型,但基本上我创建了一个 Arraylist<?扩展模块信息>moduleListModuleInfo m 对象,并尝试调用 moduleList.add(m).但是它不会编译,我收到一条错误消息,对我来说似乎有点神秘.错误消息和代码如下.有谁知道怎么回事?

I'm not sure if I am using generics correctly, but basically I created an Arraylist<? extends ModuleInfo> moduleList and ModuleInfo m objects, and tried to call moduleList.add(m). However it won't compile and I am getting an error message that seems a bit cryptic to me. The error message and code are below. Anyone else know what is wrong?

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

推荐答案

假设你创建了一个数组列表: -

Suppose you create an arraylist as: -

List<? extends Animal> list = new ArrayList<Dog>();

现在该列表引用可以指向 ArrayListArrayList 或其他任何内容.因此,当您尝试向该列表添加 Animal 引用时,编译器不确定该引用实际上指向的是在实际具体类型中使用的相同 implementationArrayList.

Now that list reference can point to an ArrayList<Dog> or an ArrayList<Cat> or whatever. So, when you try to add an Animal reference to that list, compiler is not sure that, that reference is actually pointing the same implementation that is used in actual Concrete type of ArrayList.

例如: - 您可能会在上面的 ArrayList 中添加一个指向 CatAnimal 引用,这是一场灾难.这就是编译器不允许的原因.

For e.g.: - You might add an Animal reference pointing to Cat into the above ArrayList, which is a disaster. That's why Compiler does not allow it.

Animal cat = new Cat();
list.add(cat);  // OOps.. You just put a Cat in a list of Dog

您可以只创建一个List,并且您可以向其添加任何子类型:-

You can rather just create a List<Animal>, and you can add any subtype to it: -

List<Animal> newList = new ArrayList<Animal>();
newList.add(new Cat());  // Ok. Can add a `Cat` to an `Animal` list.

上述代码有效,因为List 引用只能指向ArrayList.

The above code works because, a List<Animal> reference can only point to an ArrayList<Animal>.

因此,在您的示例中,您可以使用:-

So, in your example, you can use: -

List<ModuleInfo> list = new ArrayList<ModuloInfo>();

作为旁注,您应该始终针对 interface 而不是 implementation 进行编程.因此,始终使用 interface 的引用类型,在这种情况下为 List 而不是 ArrayList.

As a side note, you should always program to interface rather than implementation. So, always have your reference types of interface, in this case List rather than ArrayList.

这篇关于无法将 ModuleInfo 对象添加到 ArrayList<?扩展模块信息>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆