HashMap我在 FX 程序的啟動(dòng)方法中暫時(shí)顯示信息時(shí)遇到問(wèn)題。public class stack extends Application{ String mCurrentLocation; // Store current location // Map to store name as keys and easting and northing as values HashMap<String, List <Double>> dict = new HashMap<>(); public static void main(String[] args) { stack mainObject = new stack(); mainObject.run();// Method where the program is built launch(args); } public void start(Stage primaryStage) throws Exception{ System.out.println("In in startFX: "); // debugging only System.out.println(dict); // debugging only // More fx code } private void run(){ System.out.println("In main"); System.out.println(dict); //Read Northing and Easting input received from a txt file try{ Scanner scanner = new Scanner(new File("NorthingAndEastings.txt")); while(scanner.hasNext()){ List<Double> coordinates = new ArrayList<>(); String name = scanner.next(); // key Double easting = scanner.nextDouble(); coordinates.add(easting); // list(0) Double northing = scanner.nextDouble(); coordinates.add(northing); //list(1) this.dict.put(name, coordinates); } scanner.close(); } catch(FileNotFoundException e){ System.out.println("Sorry, could not open" + "'NorthingAndEastings.txt' for reading. Stopping"); System.exit(-1); } // For debugging only for (Map.Entry me : dict.entrySet()){ System.out.println("Name: "+me.getKey() + " Loc: " + me.getValue()); } }}在run()首先在 main 中調(diào)用的方法中,HashMap成員變量是從文本文件中填充的。但是當(dāng)代碼轉(zhuǎn)到我想顯示HashMap項(xiàng)目的 FX 時(shí),字典是空的。我得到{};
2 回答

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
launch
類中的方法Application
將啟動(dòng)堆棧類的一個(gè)新實(shí)例,該實(shí)例將有一個(gè)空的dict
. 如果您希望啟動(dòng)的應(yīng)用程序保留填充的值而dict
不是創(chuàng)建dict
或static map
填充您的dict
in start 方法。
希望這可以幫助。

繁星淼淼
TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
我不確定,但我認(rèn)為問(wèn)題可能是由stack mainObject = new stack();
- 您可能正在dict
為您認(rèn)為在start
.
添加回答
舉報(bào)
0/150
提交
取消