【问题标题】:Java generics: Calling generic Method "...is not applicable for the arguments..."Java 泛型:调用泛型方法“...不适用于参数...”
【发布时间】:2013-10-23 17:19:20
【问题描述】:

我有 2 个带有 <Integer, "sometype"> 的 HashMap。所以“某种类型”可能会有所不同,因此我试图使其通用。在这种情况下,我的两个变量如下

private HashMap<Integer, UI_FieldPV> values_map = new HashMap<Integer, UI_FieldPV>();
private HashMap<Integer, JComponent> input_map = new HashMap<Integer, JComponent>();

方法的第一次调用就可以了:

this.input_map = MapOperations.<JComponent> rearrengeHashMapIdx(this.input_map);

第二次调用传递一个带有&lt;Integer, CustomClass&gt;的HashMap

this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.input_map);

这给了我以下错误:

UI.MapOperations 类型的参数化方法&lt;UI_FieldPV&gt;rearrengeHashMapIdx(HashMap&lt;Integer,UI_FieldPV&gt;) 不适用于参数(HashMap&lt;Integer,JComponent&gt;)

包含泛型方法的类的编码(顺便说一句:我试图在调用类中创建一个泛型方法,但它没有工作。我是否必须创建一个嵌入式类才能使泛型方法参数工作?)

private static class MapOperations<T> {
    public static <T> HashMap<Integer, T> rearrengeHashMapIdx(HashMap<Integer, T> source) {

        HashMap<Integer, T> temp = new HashMap<Integer, T>();
        for (Integer i = 0; i < 81; i++) {
            Integer rowNum = (i / 3) % 3;
            Integer block = i / 9;
            Integer delta = (rowNum - block);
            Integer newIdx = i + (delta * 6);
            temp.put(i, source.get(newIdx));
        }
        return temp;
    }
}

那我做错了什么? 提前感谢您的帮助!

【问题讨论】:

  • 泛型方法的类型参数不是必需的,它是从传递的参数中推断出来的。

标签: java class generics methods


【解决方案1】:

编译器错误很清楚。第一次调用方法:

this.input_map = MapOperations.<JComponent>rearrengeHashMapIdx(this.input_map);

将返回一个HashMap&lt;Integer, JComponent&gt;,因为您已经给出了显式类型参数(好吧,这里并不真正需要。无论如何,T 的类型将根据您传递的HashMap 的类型推断出来)。没关系,因为您已将 input_map 声明为仅属于该类型。

然后您将 input_map 作为参数传递给下一个方法调用:

this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.input_map);

现在,根据方法的声明,方法的参数应该是HashMap&lt;Integer, T&gt; 类型。在第二个方法调用中,类型参数T 被推断为UI_FieldPV。因此,该方法需要 HashMap&lt;Integer, UI_FieldPV&gt;,但您传递的是 HashMap&lt;Integer, JComponent&gt;。当然方法调用会失败,因为两个映射不兼容。

也许,在第二次调用中,您打算将 values_map 作为参数传递。所以这可以正常工作:

this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.values_map);

请注意,方法中使用的类型参数T 与类声明中使用的类型参数T 无关,尽管在这里没有任何区别。但仅供参考。

【讨论】:

  • 哦……我的错。没看到这么明显的我把它改成了(HashMap&lt;Integer, UI_FieldPV&gt;) this.rearrengeHashMapIdx(this.values_map); 现在我得到了同样的错误(The method put(Integer, capture#4-of ?) in the type HashMap&lt;Integer,capture#4-of ?&gt; is not applicable for the arguments (Integer, capture#5-of ?)) 但现在在这行代码上(在我原始帖子中发布的方法中):temp.put(i, source.get(newIdx)); UI_FieldPV 类顺便扩展了 JPanel。
  • 好吧,我不知道为什么,但保存后,关闭并重新启动 Eclipse 后错误消失了.....问题解决了!泰
【解决方案2】:

我不明白,¿你故意放“input_map”?:

this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.input_map);

也许你需要类似的东西:

this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.values_map);

【讨论】:

  • 感谢您的帮助!我还没有见过这种愚蠢的错误。然而它只是“转移”了问题(参见第二篇文章的答案)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多