如何从此哈希表获取信息

发布于 2024-12-28 18:54:07 字数 2081 浏览 1 评论 0原文

我会尽量保持简单,但仍然可以理解。如果有任何困惑,请告诉我!

我得到了这个哈希表:

private Hashtable<String, ArrayList<String>> allChannels = new Hashtable<String, ArrayList<String>>();

我正在使用此方法插入客户端/用户:

public void connectChannel(String username, String channel) throws RemoteException{
    allChannels.put(channel, new ArrayList<String>());
    allChannels.get(channel).add(username);
}

现在我想使用此方法,以便获取连接到给定通道(参数)的用户的数组列表。这怎么可能做到呢?

@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
    return **Code needed**   
}

谢谢!

编辑:顺便说一句,连接方法进行了多次检查,但它很长,所以我没有费心粘贴它,因为我知道它工作得很好。

编辑:由于使用 return allChannels.get(channel) 似乎是正确的,所以我的 JList 肯定有其他问题。我在 GUILogic 中使用了这些方法来运行 JList。我已经让另外 2 个 JList 以同样的方式工作。

DefaultListModel usersInChanDLM = new DefaultListModel();

public DefaultListModel getUsersInChannelAsDefaultListModel() throws RemoteException{
    if(!(getChannel() == null)){
        for(String a : cf.getUsersInChannel(getChannel())){
            usersInChanDLM.addElement(a);
            System.out.println(a);
        }
    }
    return usersInChanDLM;
}

void updateUsersInChannelJlist(JList jList3) throws RemoteException {
    usersInChanDLM.clear();
    usersInChanDLM.removeAllElements();
    for(Client c : cf.getClients()){
        if(!(usersInChanDLM.contains(c.findName()))){
            usersInChanDLM.addElement(c.findName());
        }
    }
    jList3.setModel(usersInChanDLM);
}

所以,我显然能够看到最后一个方法是搞砸的,因为它添加了列表中的所有客户端。但它应该怎么说呢?

解决方案:

所以,我成功了!

    public void updateUsersInChannelJlist(JList jList3) throws RemoteException {
        usersInChanDLM.clear();
        usersInChanDLM.removeAllElements();
        for(String s : cf.getUsersInChannel(channel)){
            if(!(usersInChanDLM.contains(s))){
                usersInChanDLM.addElement(s);
            }
        }
        jList3.setModel(usersInChanDLM);
   }

Ill try to keep this simple, but still understandable. If there's any confusion, let me know!

I got this hashtable:

private Hashtable<String, ArrayList<String>> allChannels = new Hashtable<String, ArrayList<String>>();

I'm inserting clients/users with this method:

public void connectChannel(String username, String channel) throws RemoteException{
    allChannels.put(channel, new ArrayList<String>());
    allChannels.get(channel).add(username);
}

Now i want to use this method, in order to get a arraylist with users connected to a given channel(argument). How could this be done?

@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
    return **Code needed**   
}

Thanks!

EDIT: Btw guys, the connectmethod got several checks, but its pretty long, so i didn't bother pasting it, since i know that its working great.

EDIT: Since theres seems to be correct using return allChannels.get(channel) theres gotta be something else wrong with my JList. I've used these methods in my GUILogic to get the JList running. I've made 2 other JList working the same way.

DefaultListModel usersInChanDLM = new DefaultListModel();

public DefaultListModel getUsersInChannelAsDefaultListModel() throws RemoteException{
    if(!(getChannel() == null)){
        for(String a : cf.getUsersInChannel(getChannel())){
            usersInChanDLM.addElement(a);
            System.out.println(a);
        }
    }
    return usersInChanDLM;
}

void updateUsersInChannelJlist(JList jList3) throws RemoteException {
    usersInChanDLM.clear();
    usersInChanDLM.removeAllElements();
    for(Client c : cf.getClients()){
        if(!(usersInChanDLM.contains(c.findName()))){
            usersInChanDLM.addElement(c.findName());
        }
    }
    jList3.setModel(usersInChanDLM);
}

So, im obviously able to see that the last method is the one screwing this up, since it adds all the clients in the list. But what should it say instead?

Solution:

So, i made it!

    public void updateUsersInChannelJlist(JList jList3) throws RemoteException {
        usersInChanDLM.clear();
        usersInChanDLM.removeAllElements();
        for(String s : cf.getUsersInChannel(channel)){
            if(!(usersInChanDLM.contains(s))){
                usersInChanDLM.addElement(s);
            }
        }
        jList3.setModel(usersInChanDLM);
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

献世佛 2025-01-04 18:54:07

要么我错过了一些非常明显的东西,要么你只是忽略了答案(你的代码中已经有了这个答案!)

return allChannels.get(channel);

你应该考虑的一件事是,如果你一遍又一遍地调用这个 connectChannel 方法,你将不断地破坏你的数组列表(因为你每次调用该方法时都对其调用 new ),因此实际上每个通道注定始终有一个用户。

您应该检查数组列表是否为空,如果不是,则不必费心重新实例化它(除非我错过了您想要做的事情,我很可能就是这样)。

Either I'm missing something really obvious, or you're just overlooking the answer (which you already have in your code!)

return allChannels.get(channel);

One thing you should consider is that if you're calling this connectChannel method over and over you will constantly be destroying your arraylist (since you're calling new on it, every time you call that method), so in effect each channel is doomed to have one user in it at all times.

You should check to see if the array list is null and if it's not, don't bother to re-instantiate it (unless I'm missing what you're trying to do, which I very well might be).

假面具 2025-01-04 18:54:07

这应该可以工作

@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
    return allChannels.get(channel);   
}

这一行(你在那里有它)的工作方式相同 - get() 方法为你提供 ArrayList ,然后你添加一个项目。

allChannels.get(channel).add(username);

This should work

@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
    return allChannels.get(channel);   
}

This line (you have it there) works the same way - get() method gives you the ArrayList and than you add an item.

allChannels.get(channel).add(username);
幽梦紫曦~ 2025-01-04 18:54:07

您的 connectChannel 方法有错误。您不应该每次都创建 ArrayList。

public void connectChannel(String username, String channel) throws RemoteException{
    ArrayList<String> ch = allChannels.get(channel);
    if (ch==null){
        ch = new ArrayList<String>();
        allChannels.put(channel, ch);
    }   
    ch.add(username);
}

There is a error in your connectChannel method. You should not create ArrayList each time.

public void connectChannel(String username, String channel) throws RemoteException{
    ArrayList<String> ch = allChannels.get(channel);
    if (ch==null){
        ch = new ArrayList<String>();
        allChannels.put(channel, ch);
    }   
    ch.add(username);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文