簡體   English   中英

將哈希圖的鍵/值輸入到對象中

[英]inputting key/values of hashmap into an object

可以說我的程序中具有以下哈希表設置。.我想獲取哈希表的輸入並將其存儲到對象中。 當前的哈希圖列表太長,無法放入主代碼中,因此我試圖從單獨的目標文件中讀取輸入內容以限制主代碼的長度。 您如何建議我去做呢?

謝謝!

int i = input.nextInt(); 
Map<Character,Integer> map = new HashMap<Character,Integer>();                         

map.put('A', i*2);                        
map.put('B', i*2);
map.put('C', i*2);
map.put('D', i*4);
map.put('E', i*2);
map.put('F', i*3);
map.put('G', i*2);
map.put('H', i*6);
                  and so on forth down to Z and other 20 other characters...

你的意思是這樣的:

int i=1000;//anything what you like
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int x=65;x<=90;x++){
      char c=(char)x;
      map.put(c, i*2);
}    

假設這些乘數不必更改,則可以執行以下操作。

int[] multipliers = {2,2,2,4,2,3,6,...};
char chars[] = {'A','B',...}; /// or if they are in ascii order you dont need to specify this
for (int j=0;j<chars.length;j++){
    map.put(chars[j],i * multipliers[j]);
}

只需確保兩個數組的大小相同即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM