我有一個包含用戶和頻道的聊天程序。我的下一個任務是獲取一個用戶所在的頻道列表。該怎麼做?使用Hashtable,Arraylist。從他們那裏獲取信息
這裏是代碼截至目前:
ChatFrontImpl:
private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
private ArrayList<Client> clients;
public synchronized boolean registerClient(Client client, String password) throws RemoteException {
if(!u.logIn(client.findName(), password)){
System.out.println("Wrong username or password!");
return false;
}
if (!clients.contains(client)) {
try {
clients.add(client);
updateJlist();
System.out.println(client.findName() + " registered.");
}
catch (Exception e){
System.out.println("error in method registerClient(): " + e);
}
return true;
}else
return false;
}
public void connectChannel(String username, String channel) throws RemoteException{
if(isUserRegistered(username)){
if (!channels.containsKey(channel)) {
String message = "User " + username + " entered the channel";
channels.put(channel, new ArrayList<String>());
channels.get(channel).add(username);
notifyChannelSystem(channel, "SYSTEM", message);
notifySelf(username, "Write /? for avaliable commands");
}
else{
if(channels.get(channel).contains(username)){
}
else {
channels.get(channel).add(username);
String message = "User " + username + " just entered the channel";
notifyChannelSystem(channel, "SYSTEM", message);
}
}
}
}
你可以擴展你的問題,包括變量的定義** kanal **請。如果** kanal **實際上應該是**頻道**,該方案纔有意義。 –
哦,代碼通常是挪威語,但我翻譯(顯然只是它的一部分)。該變量應該是渠道,無論它說kanal ofc :) – sindrem
我更正了變量+改變了方法無效,因爲我不需要返回真的 – sindrem