【问题标题】:The method put is not applicable for the argumentsput 方法不适用于参数
【发布时间】:2016-04-28 20:55:54
【问题描述】:

不太清楚为什么 put 方法会出现此错误。帮助将不胜感激。

这段代码

HashMap<ArrayList<String>,ArrayList<Integer>> magicshopitems = new HashMap<ArrayList<String>,ArrayList<Integer>>();
magicshopitems.put("items", itemlist);
magicshopitems.put("amount", itemlistamount);

生产

The method put(ArrayList<String>, ArrayList<Integer>) in the type HashMap<ArrayList<String>,ArrayList<Integer>> is not applicable for the arguments (String, ArrayList<String>)

【问题讨论】:

  • String 不是 ArrayList&lt;String&gt;

标签: java arraylist hashmap


【解决方案1】:

您正在尝试将字符串放入 ArrayList。

你需要先把String放到一个列表中;

ArrayList<String> list = new ArrayList<String>();
list.add("items");

magicshopitems.put(list, itemlist);

旁注

在构造此类集合时,最好使用父接口类。

代替

HashMap<ArrayList<String>,ArrayList<Integer>> magicshopitems = 
    new HashMap<ArrayList<String>,ArrayList<Integer>>();

你可以拥有

Map<List<String>, List<Integer>> magicshopitems = 
    new HashMap<List<String>, List<Integer>>();

【讨论】:

  • 或者更有可能,将 hashmap 更改为 HashMap&lt;String, ArrayList&lt;Integer&gt;&gt;
  • 谢谢,问题已经解决,感谢帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 2018-08-26
  • 1970-01-01
  • 2018-07-19
  • 2012-08-30
相关资源
最近更新 更多