【问题标题】:Java - Type mismatch (But both are from same type?!): cannot convert from HashMap<Integer,ArrayList<Integer>> to HashMap<Integer,ArrayList<Integer>>Java - 类型不匹配(但两者都来自同一类型?!):无法从 HashMap<Integer,ArrayList<Integer>> 转换为 HashMap<Integer,ArrayList<Integer>>
【发布时间】:2019-12-24 23:29:32
【问题描述】:

编辑:问题已解决:请参阅 Karim SNOUSSI 的答案和我在下面的评论。


这是我在堆栈溢出时的第一个问题,所以我可能不会在开始时把所有事情都做好。对此感到抱歉。此外,我是 java 和一般编程的新手。

我遇到了一个奇怪的错误,无法真正理解解决方案是什么。

在尝试将 hashMap 从一个类传递到另一个类时,IDE eclipse 说: 类型不匹配:无法从 HashMap> 转换为 HashMap>

但是如果我做对了,两者都是同一类型,所以不知道是什么问题。

这是我的代码:我只发布了其中的一部分,所以不要乱七八糟

我的班级中有一个名为 graph.class 的 HashMap, 它用于方法 public HashMap> getAllShortestPaths() 其中将计算从每个节点到图中任何其他节点的所有最短路径 并存储到 hashMap 中以供进一步处理。 效果很好,当将地图打印到屏幕上时,它会正确显示所有信息,如果我想从内部的方法中做到这一点。

但我的目的是将这个 hashMap 传递给另一个类,在那里我将收集我对图表的整个分析并将其保存到一个新文件中。

public class Graph {
.
.
    private HashMap<Integer, ArrayList<Integer>> shortestPathsMap = new HashMap<Integer, ArrayList<Integer>>();

。 . .

public HashMap<Integer, ArrayList<Integer>> getShortestPathsMap() { return shortestPathsMap; }
.
.
.
    public void getAllShortestPaths() {
        for(int i = 0; i < getNodeCount(); i++) {
            ArrayList<Integer> shortestPathMapValues = new ArrayList<>();   // saves ... 

            for(int n = i; n < getNodeCount(); n++) {
                shortestPathMapValues.add(n);                               // the corresponding node id's and ..
                shortestPathMapValues.add((int) shortestPath(i,n));         // the outcome of shortestPath() calculation
            }

            shortestPathsMap.put(i, shortestPathMapValues);                 // saves the first node id and the corresponding values
        }
    }
.
. 

所以为了测试,我将它传递给 Main.class 并且确实想将它打印到屏幕上:

public HashMap<Integer, ArrayList<Integer>> getShortestPathsMap() { 
        return shortestPathsMap; 
}

public class Main {

    public static void main(String[] args) {
.
.
.
G.getAllShortestPaths();

                    HashMap<Integer, ArrayList<Integer>> spMap = new HashMap<Integer, ArrayList<Integer>>();
                    spMap = G.getShortestPathsMap();

                    // iterate and display values
                    for(Entry<Integer, ArrayList<Integer>> entry : spMap.entrySet()) {
                        int key = entry.getKey();
                        ArrayList<Integer> values = entry.getValue();

                        System.out.println("Key = " + key);
                        System.out.println("Values = " + values);
                    }
.
.
. 

在 Main.class 中,在以下行: spMap = G.getShortestPathsMap(); IDE 显示

类型不匹配:无法从 HashMap 转换> 到HashMap>

但是:

HashMap<Integer, ArrayList<Integer>> spMap = new HashMap<Integer,Integer, ArrayList<Integer>>();

HashMap<Integer, ArrayList<Integer>> shortestPathsMap = new HashMap<Integer, ArrayList<Integer>>();

spMap 和 shortestPathsMap 是同一个类型,不是吗?

我很高兴收到任何有用的回复,并提前感谢您。

【问题讨论】:

  • 你能把getShortestPathsMap();的代码贴出来吗?
  • 它只是一个简单的吸气剂。我用 getter 方法试过一次,用 getAllShortestPaths() 方法中的 return 参数试过一次,但都不起作用。
  • 请清理您的问题。您的类型都已损坏。我看到很多“HashMap>”,这表明某些软件试图修改您的问题,可能是出于“HTML sanitization”的原因。
  • 您粘贴的代码不完整。 G.getShortestPathsMap() – “G”在哪里定义? “getShortestPathsMap”在哪里定义?许多其他事情也没有排队。看看这个:How to create a Minimal, Reproducible Example
  • 是的,不幸的是,我尝试在此处复制粘贴代码,但它一直被自动修改并且清理起来很麻烦。

标签: java arraylist collections maps


【解决方案1】:

尝试在你的 main 方法中替换它

HashMap<Integer, ArrayList<Integer>> spMap = new HashMap<Integer, ArrayList<Integer>>();
spMap = G.getShortestPathsMap();

有了这个

Map<Integer, ArrayList<Integer>> spMap = G.getShortestPathsMap();

【讨论】:

  • 已经尝试过了,不幸的是得到了相同的类型不匹配消息。不过谢谢
  • 我真的不知道它有什么问题,但尝试用spMap = new HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;(G.getShortestPathsMap());替换它
  • 太好了,非常感谢您的提示。这对我有用:HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; spMap = new HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;(G.getShortestPathsMap());
  • 不幸的是它首先工作但在多次运行程序后没有任何错误它突然说: 构造函数 HashMap>(HashMap> ) 未定义
  • 我将其更改为 HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; spMap = new HashMap(G.getShortestPathsMap());,尽管有警告 HashMap 是原始类型,但它仍然有效。对泛型类型 HashMap 的引用应该被参数化
猜你喜欢
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
相关资源
最近更新 更多