2013-04-10 56 views
2

我想編寫Java(Eclipse中)內的登錄界面內,而當我在玩家登錄創想我能夠使用像這樣的數組:如何存儲整數字符串

例如:

int x = userX[username]; 
int y = userY[username]; 

,我希望能夠建立這樣的: 例如:

userX[username] = x; 
userY[username] = y; 

而且,由於一個玩家的用戶名可以是任何東西(例如:dark09.loser)竟被我d喜歡能夠存儲對象內部的整數。 感謝您的幫助。 (而對於英語不好對不起)

+1

?你會想查找HashMap。更新:bmorris已經爲此寫了下面的答案 – CodeGuy 2013-04-10 23:10:10

+1

這還不清楚。什麼是'userX'和'userY'數組?什麼是'用戶名'? – raptortech97 2013-04-10 23:10:12

回答

2

使用Map

Map<String, Integer> map = new HashMap<String, Integer>(); 

map.put("username1", x); 
1

使用Hashtable<String,Integer>,應該做你想做的。

+2

'HashTable'是同步的,因此速度很慢。地圖比較好。 – 2013-04-10 23:12:06

+0

字符串是大寫字母,原始類型不能是通用類型。 – gparyani 2013-04-10 23:12:12

3

你可能需要一個Map<String, Integer>,那麼你可以做這樣的事情

final Map<String, Integer> myMap = new HashMap<>(); 
final Integer x = myMap.get(username); 
myMap.put(username, x); 
要使用字符串作爲索引
+0

根據我的意見:) – CodeGuy 2013-04-10 23:11:53

+0

這是驚人的有多少問題可以通過「地圖」解決...... – 2013-04-10 23:13:27

+0

FTFY:令人驚訝的是,有多少問題可以通過Map來解決? – Nicholas 2013-04-10 23:29:32