2012-06-03 54 views
0

所以,我抓住了什麼,我相信樹形圖作爲一個字符串,它看起來像這樣:獲得在一個TreeMap特定值

{Username1={password=password1}, Username2={password=password2}} 

我怎麼會去獲取價值「用戶名」和「USERNAME2」,以及作爲用戶名1的密碼(密碼1)和用戶名2的密碼(密碼2)?有沒有辦法迭代數組中的值或類似的東西?

任何幫助將不勝感激。

謝謝。

+0

這是在Java中?如果是這樣,請通過編輯您的問題來添加相關標籤。 – assylias

+0

啊,是的,對不起。 :) –

回答

3

TreeMap是訂購的Map。所有地圖有那些3種方法:

map.keySet(); //returns a Set containing the keys (Username in your case) 
map.values(); //returns a Collection containing the values (the passwords in your case) 
map.entrySet(); //retrurns a Set of entries (an entry is a key + value) 

如果你想在一個循環中同時訪問,最好的辦法是通過的entrySet:

for (Map.Entry<UserName, Password> e : map.entrySet()) { 
    UserName user = e.getKey(); 
    Password pwd = e.getValue(); 
} 
+0

我將如何迭代每個值? –

+0

@DuncanPalmer讓我知道如果這不能回答你的問題。 – assylias

+0

如何在Map.Entry中分配鍵和值參數