我有两个Hashtables和一个LinkedList,如:
Hashtable<Character, Hashtable<String, LinkedList<String>>> hashTab = new Hashtable();
Hashtable<String, LinkedList<String>> string_list = new Hashtable();
LinkedList<String> data_list = new LinkedList();
现在我有一个函数,通过它我可以在哈希表和链表中传递值
public String createPlayerAccount(String FirstName, String LastName, int Age, String Username, String Password, String IPAddress)
{
char username_first_char = Username.charAt(0);
try
{
boolean username_exists = hashTab.get(username_first_char).containsKey(Username);
if(!username_exists)
{
String data_string = FirstName + " " + LastName + " " + Age + " " + Password + " " + IPAddress + playerStatus; //Pass user details to a string
data_list.add(data_string); //Add user details to the linked list
string_list.put(Username, data_list);
hashTab.put(username_first_char, string_list);
return("Dear " + FirstName + ", You have successfully registered");
}
else
{
return("Username already exists");
}
}
catch(Exception e)
{
String data_string = FirstName + " " + LastName + " " + Age + " " + Password + " " + IPAddress +" "+ playerStatus; //Pass user details to a string
data_list.add(data_string); //Add user details to the linked list
string_list.put(Username, data_list);
hashTab.put(username_first_char, string_list);
return("Username successfully added \n ");
}
}
默认情况下,playerStatus设置为0。现在我正在为Signin()创建另一个函数,我希望将playerStatus的值更新为1.我该怎么办?
登入():
public String playerSignIn(String Username, String Password, String IPAddress)
{
char username_first_char = Username.charAt(0);
try
{
String user_profile;
boolean username_exists = hashTab.get(username_first_char).containsKey(Username);
if(username_exists)
{
playerStatus = "1";
//String data_string = Password + " " + IPAddress +" "+ playerStatus; //Pass user details to a string
//data_list.add(data_string); //Add user details to the linked list
//string_list.put(Username, data_list);
//hashTab.put(username_first_char, string_list); //Add user details to the linked list
//return();
//hashTab.put(username_first_char, string_list);
//user_profile = hashTab.get(username_first_char).get(Username).get(0);
return(user_profile);
/*String data_string = Username + " " + Password + " " + IPAddress +" "+ "1"; //Pass user details to a string
data_list.add(data_string); //Add user details to the linked list
string_list.put(Username, data_list);
hashTab.put(username_first_char, string_list);
String get_user_data = hashTab.get(username_first_char).get(Username).get(0);
String[] user_record = get_user_data.split(" ");
String users_status = user_record[5];*/
//return("User status updated: " + user_record[5]);
}
else
{
return("Invalid username or password");
}
}
catch(Exception e)
{
return("PAWNED");
}
}
注意:这是代码的服务器端。我正在建立一个客户端 - 服务器分布式系统。
答案 0 :(得分:1)
如评论中所述,您需要创建一个POJO(对象类)来保存有关给定玩家的信息。我将该名称命名为Player。如果你需要一个包含播放器信息的字符串,要发送给客户端,只需要在Player类上使用toString()方法。
当我开发多人游戏时,在服务器端我通常使用ConcurrentHashMap来存储信息。如果您使用的消息传递系统需要哈希表,那么您将遇到令人头疼的同步问题,如果您不知道自己在做什么,这可能会导致死锁。
开始重构您的代码。在使用Player类之后,代码应该更具可读性,并且您应该能够找到另一个POJO可能有用的位置。