2013-07-07 29 views
0

我正在調整一些XML文件,並設置一些關係檢索值。雖然這樣做我有以下需求:在運行時命名變量

我有一個字符串,如String nextrelation,它的值是在運行時(從xml中提取)導出的。現在,是有可能有一個ArrayList這樣的:

ArrayList<String> nextrelation(value of the string) = new ArrayList<>(); 

有點像寫照嗎? (如果我有點感覺:))。

請提出出路;我希望有一些辦法可以做到這一點。

回答

2

變量名稱對程序員來說只是一種方便,在編譯代碼後甚至不會記錄。因此,不可能像這樣「動態地」命名變量。您可以嘗試使用Map存儲在標識值對,而不是:

Map<String, ArrayList<String>> map = new HashMap<>(); 
map.put(nextrelation, new ArrayList<String>()); 
... 

現在,檢索nextrelation對應,您可以使用map.get(nextrelation)名單。

0

craeta一個HashMap和使用它像以下:

HashMap<String,List<String>> hm = new HashMap<String,List<String>>(); 

,那麼你可以在你的程序中使用它的方式如下:

hm.put(nextrelation(value of the string),List<String>); 

hm.get(nextrelation(value of the string));