无法将一个ModuleInfo对象添加到ArrayList<扩展ModuleInfo> [英] Can't add a ModuleInfo object to ArrayList<? extends ModuleInfo>

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

问题描述

我不确定我是否正确使用泛型,但基本上我创建了 Arraylist< ;?扩展ModuleInfo> moduleList ModuleInfo m 对象,并试图调用 moduleList.add(m)。但它不会编译,我收到一条对我来说似乎有点神秘的错误消息。错误消息和代码如下。任何人都知道什么是错的?

  void load(){
ArrayList<扩展ModuleInfo> moduleList = new ArrayList();
迭代器<?扩展ModuleInfo> iter_m;
ModuleInfo m;

//获取依赖于这个模块的模块
//获取所有模块的列表并迭代每一个模块
iter_m = Lookup.getDefault()。lookupAll(ModuleInfo.class ).iterator(); (iter_m.hasNext()){
m = iter_m.next();
//循环遍历模块依赖关系并检查对这个模块的依赖关系
for(依赖d:m.getDependencies()){
//如果找到了,模块到列表
if(d.getName()。equals(GmailAuthManager.class.getPackage()。getName())){
moduleList.add(m);
休息;




code

$ b

错误如下所示:

error:找不到合适的方法add(ModuleInfo)
moduleList.add(米);
方法ArrayList.add(int,CAP#1)不适用
(实际和形式参数列表长度不同)
方法ArrayList.add(CAP#1)不适用
(实际参数ModuleInfo不能通过方法调用转换转换为CAP#1)
其中CAP#1是新鲜的类型变量:
CAP#1扩展了ModuleInfo从?扩展ModuleInfo


解决方案

假设您创建一个arraylist为: -

 列表< ;?延伸动物> list = new ArrayList< Dog>(); 

现在列表引用可以指向 ArrayList< Dog> ArrayList< Cat> 或其他。因此,当您尝试向该列表中添加 Animal 引用时,编译器不确定该引用实际上是否指向相同的实现实际使用的 ArrayList



Animal 引用指向 Cat 到上面的ArrayList中,这是一场灾难。这就是为什么Compiler不允许它。

  Animal cat = new Cat(); 
list.add(cat); // OOps ..您只需将一只猫放入狗的列表中

您可以创建一个 List< Animal> ,您可以添加任何子类型: - $ / $>
$ b $ $ p $ 列表与LT;动物及GT; newList = new ArrayList< Animal>();
newList.add(new Cat()); // 好。可以将猫添加到动物列表。

上面的代码起作用是因为 List< Animal> reference只能指向 ArrayList< Animal>






因此,在你的例子中,你可以使用: -

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

作为一个附注,您应该总是编程为 interface 而不是执行。因此,总是要有接口的引用类型,在这种情况下 List 而不是 ArrayList


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 is a follows:

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>();

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.

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

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.

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>();

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&lt;扩展ModuleInfo&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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