問題描述
我正在使用 Android Studio 1.4.1,我剛剛創建了一個 Hashmap,并且正在學習如何填充和操作它的教程(關于 java).但是,我收到無法解析符號放置"錯誤,并且放置"命令為紅色.我添加的圖像顯示了自動完成快照,盡管導入了 java.util.HashMap,但自動完成中沒有可用的put"命令.可用的命令也以紅色顯示.我嘗試使用它們而不是put"命令.我一直有這種類型的問題.任何人都可以幫忙嗎?提前謝謝你...
I am working with Android Studio 1.4.1 and I had just created a Hashmap and was following a tutorial (on java) on how to populate and manipulate it. However I get a 'cannot resolve symbol put' error and the "put" command is in red. The image I added shows the auto complete snapshot and although java.util.HashMap is imported there is no "put" command that is available in autocomplete. The available commands also are showing in red. I tried to use them instead of the "put" command. I keep having this type of problem all along. Can anyone help? Thank you in advance...
import java.util.HashMap;
HashMap<String, String> pozisyon = new HashMap<String, String>();
pozisyon.put("SKale", "a8");
推薦答案
您不能在方法之外的 HashMap 字段中添加元素.這樣的事情是行不通的:
You cannot add elements in HashMap fields outside of methods. Things like this wont work:
public class Class {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("one", "two");
}
如果你想實現它,把它放在構造函數中,像這樣:
If you want to achieve that, put it in the constructors, like so:
public class Class {
HashMap<String, String> hashMap = new HashMap<String, String>();
public Class() {
hashMap.put("one", "two");
}
}
您可以使用 static
塊來執行此操作.
Other way you can do it is in a static
block.
這篇關于如何在出現“無法解析放置符號"錯誤時向 Hashmap 添加鍵和值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!