“cannot find symbol” 在尝试从映射中的 ArrayList 获取对象时。

huangapple go评论86阅读模式
英文:

"cannot find symbol" while trying to get an object from an arrayList in a map

问题

我尝试修改一个在Map中的ArrayList,但它一直给我“找不到符号错误”。

这是代码:

Map<String, ArrayList<Item>> inventories = new TreeMap<>();
inventories.put("itms_eqped", new ArrayList<Item>(6));
System.out.println(inventories.get("itms_eqped").get(0));

这是错误信息:

./Main.java:29: error: cannot find symbol
System.out.println(inventories.get("itms_eqped").get(0));
                                        ^
symbol:   method get(int)
location: class Object

我也尝试添加东西到ArrayList但我得到了相同的错误:“找不到符号”。
英文:

I am trying to modify an ArrayList in a map, but it keeps giving me the "Symbol not found error"

Here is the code:

Map inventories = new TreeMap&lt;String, ArrayList&lt;Item&gt;&gt;();
inventories.put(&quot;itms_eqped&quot;, new ArrayList&lt;Item&gt;(6));
System.out.println(inventories.get(&quot;itms_eqped&quot;).get(0));

Here is the error

./Main.java:29: error: cannot find symbol
System.out.println(inventories.get(&quot;itms_eqped&quot;).get(0));
                                                ^
symbol:   method get(int)
location: class Object

I've also tried adding stuff to the arrayList and I'm getting the same error: "Cannot find symbol".

答案1

得分: 1

你需要声明键和值的类型,这样 Java 才能知道应该使用哪种类型。否则,Java 将把你的键和值视为 Object 类型。

Map<String, ArrayList<Item>> inventories = new TreeMap<>();
英文:

You have to declare type of your key and value for Java to know which type it should use. Otherwise, java will consider your key and value as type Object.

Map&lt;String, ArrayList&lt;Item&gt;&gt; inventories = new TreeMap&lt;&gt;();

huangapple
  • 本文由 发表于 2023年6月6日 08:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定