我在一個存儲String鍵和boolean值的類中有一個 Map。然后,我從函數(shù)返回地圖getMap()。public class FacilityMachines { private static Map<String, Boolean> map = new HashMap<String, Boolean>(); public Map getMap(){ return map; }在下面的課程中,我試圖獲取該地圖,然后將其保存到外部文件中,我還在FacilityMachines那里實例化:public class WriteFile { FacilityMachines fm = new FacilityMachines(); private Map<String, Boolean> m = new HashMap<String, Boolean>();}在WriteFile中,我試圖將地圖解析成一個新的 HashMap:public void saveFacilityInfo() { for (Map.Entry<String, Boolean> j: fm.getMap().entrySet()){ String s = j.getKey(); boolean b = j.getValue(); oStream.println(i + ": " + s + " = " + b + ". "); }}oStream只是我的變量PrintWriter。以上產(chǎn)生Object cannot be converted to Entry<String, Boolean>錯誤。如果我將方法簽名更改saveFacilityInfo為saveFacilityInfo(FacilityMachines fm),然后使用該fm變量嘗試在該行獲取地圖,for (Map.Entry<String, Boolean> j: fm.getMap().entrySet())那么我會從接口中獲得cannot find symbol所有函數(shù)的a : 、和。EntryentrySet()getKey()getValue()在有人問之前,我已經(jīng)導(dǎo)入了HashMapand Map,并且還嘗試使用 onlyimport java.util.*;來導(dǎo)入所有內(nèi)容以防萬一。我也嘗試過擴展并FacilityMachines得到WriteFile了相同的結(jié)果。
1 回答

茅侃侃
TA貢獻1842條經(jīng)驗 獲得超22個贊
您需要在 FacilityMachines 類的 getMap() 方法上以正確的類型返回地圖
public class FacilityMachines {
private static Map<String, Boolean> map = new HashMap<String, Boolean>();
public Map<String, Boolean> getMap(){
return map;
}
}
添加回答
舉報
0/150
提交
取消