0

在我的应用程序中,我应该在线程上运行时从列表中删除一个字符串,但是我得到了这样的异常,

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: item gh not found in list
    at java.awt.List.remove(Unknown Source)
    at org.sample.ChatClient$updateClient$1.run(ChatClient.java:200)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

代码:

final Map<String, String> liHashMap=list;
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        for (Entry<String, String> entry : liHashMap.entrySet()) {
            String client_Name=entry.getKey();
            if(!checkList.containsKey(client_Name)) {
                lst.add(client_Name + "\n");
                checkList.put(client_Name, ipAddress);
            }
        }
        for (Entry<String, String> entry : checkList.entrySet()) {
            String client_Name=entry.getKey();
            if(!liHashMap.containsKey(client_Name)){
                lst.remove(client_Name);//Remove string from list
                checkList.remove(client_Name);
        }
    } 
4

2 回答 2

1

问题就在这里。改变这个:

if(!liHashMap.containsKey(client_Name)){
                lst.remove(client_Name);//Remove string from list
                checkList.remove(client_Name);
        }

对此:

if(liHashMap.containsKey(client_Name)){
                lst.remove(client_Name);//Remove string from list
                checkList.remove(client_Name);
        }

我假设您的哈希映射是要从您的列表中删除的项目的后备存储。正确的?因此,您应该只删除位于散列映射中并因此位于列表中的键。

如果这不是它的工作方式,那么您需要维护一个应该删除的项目列表,并且您已经验证了这些项目实际上在列表中,然后删除它们。像这样:

if(toBeRemovedMap.containsKey(client_Name)){
                lst.remove(client_Name);//Remove string from list
                checkList.remove(client_Name);
        }
于 2013-05-08T14:08:26.413 回答
0

如果IllegalArgumentException传递的值在java.awt.List.

如果你使用,如果传递的值存在 else java.util.List,那将返回。truefalse

于 2013-05-08T14:08:53.020 回答