英文:
"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<String, ArrayList<Item>>();
inventories.put("itms_eqped", new ArrayList<Item>(6));
System.out.println(inventories.get("itms_eqped").get(0));
Here is the error
./Main.java:29: error: cannot find symbol
System.out.println(inventories.get("itms_eqped").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<String, ArrayList<Item>> inventories = new TreeMap<>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论