0
對於學校我建立一個多人遊戲。在這個遊戲中,我有一個在大廳裏的球員名單。該列表包含玩家的ID和玩家的名字。 Thoose是我的價值觀。作爲關鍵我會把大廳的關鍵。一起形成我正在使用的Hashmap。有沒有辦法得到一個整數howmany值有一個hashmap中有相同的密鑰?
當我想連接到大廳,我必須知道如果播放器allready是在hashmap中,所以我需要一個foreach循環來檢查。但我不知道該怎麼做,或者即使這是可能的。
private HashMap<Integer, List<String>> playersInLobby;
public IRemotePublisherForListener connect(int playerID, String spriteName, int lobbyID)
{
boolean found = false;
for (int i = 0; playersInLobby.containsKey(lobbyID);)
{
if (playersInLobby.get(lobbyID).get(0) == playerID + "")
{
found = true;
}
}
if (!found)
{
playersInLobby.put(lobbyID, new ArrayList<>(Arrays.asList(playerID + "", spriteName)));
}
return publisher;
}
'值HashMap中具有相同key',只有1。你不能有一個HashMap重複鍵,即點;) – AxelH
你'put'邏輯是相當也很遙遠 - 這樣,每個大廳裏最多隻能有一名球員。什麼是「發佈者」和應該是什麼樣的for循環?你現在試圖阻止球員兩次進入大堂或同時進入兩個大廳嗎? – luk2302
您可以使用[Guava Multimap](https://github.com/google/guava/wiki/NewCollectionTypesExplained#Multimap) –