【问题标题】:How to typecast map.getValue() to ArrayList<Integer>()? [duplicate]如何将 map.getValue() 类型转换为 ArrayList<Integer>()? [复制]
【发布时间】:2019-08-11 14:09:05
【问题描述】:

我想要做的是,将索引存储在它重复的当前元素的数组列表中,我已经这样做了,现在我想检查一下。

HashMap<Integer,ArrayList<Integer>> hmap = new HashMap<>();
        for(int i=0;i<arr.length;i++)
        {
            if(!hmap.containsKey(arr[i]))
            {
                ArrayList<Integer> a1 = new ArrayList<>();
                hmap.put(arr[i],a1);
            }
            else
                hmap.get(arr[i]).add(i);
        }

        for(Map.Entry m:hmap.entrySet())
        {
            ArrayList<Integer> alist = {Here it is showing erorr.}m.getValue();
            System.out.println(m.getKey()+" ");
            for(int i=0;i<alist.size();i++) 
                System.out.print(alist.get(i)+" ");
        }

【问题讨论】:

    标签: java arraylist collections casting


    【解决方案1】:

    您必须在 foreach 循环中指定您的 generic types(与 Map 中相同)。

    for (Map.Entry<Integer, ArrayList<Integer>> m : hmap.entrySet())
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2013-11-26
      • 1970-01-01
      • 2014-04-30
      • 2011-08-07
      相关资源
      最近更新 更多