0

起初,如果删除数组列表中的元素是列表中唯一的元素,则会出现问题,但现在在删除数组列表的最后一个位置时出现异常问题。处理这个问题的最佳方法是什么?

编辑:回顾一下,检查它是否是最后一个元素并放入一个虚拟元素来保持唯一的位置会起作用。

编码:

  public void deleteCustomer(){
            String id = null;
            boolean c = false; //true if id is found
            int remember = 0;      //Remembers the deleted index

            id = JOptionPane.showInputDialog(null,"input the if of whome you want to delete",
                    "input id", JOptionPane.PLAIN_MESSAGE);

            int id2 = Integer.parseInt(id); //new int id.


            for(int i = 0; i < customers.size(); i++){

                if(id2 == customers.get(i).getID()){

                    if(customers.size() == 1){
                        System.out.println("test one person");
                        customers.get(i).setDate(null);
                        customers.get(i).setID(0);
                        customers.get(i).setName(null);
                        customers.get(i).setPeople(0);
                    }
                    else{   
                     customers.remove(i);
                    }
                    c = true;
                    remember = i;

                if(c == true)
                break;
            }
            }

            if(c == true){

                int i1 = JOptionPane.showConfirmDialog(null,"the customer "
                       + customers.get(remember).getName() + " has been deleted.",
                            "input people", JOptionPane.PLAIN_MESSAGE);

            }
            else{

                int i1 = JOptionPane.showConfirmDialog(null,"the customer could not be found," +
                        " please check your id",
                                "input people", JOptionPane.PLAIN_MESSAGE);

            }



        }

错误

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at MainFrame.deleteCustomer(MainFrame.java:360)
    at MainFrame$4.actionPerformed(MainFrame.java:170)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.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)
4

4 回答 4

2

不要在使用 for 循环迭代集合时从集合中删除元素。改用迭代器,它在大多数情况下实现该remove方法。

Iterator<Customer> it = customers.iterator();
while(it.hasNext()) {
 if(it.next().getId() == id2) {
  it.remove();
 }
}
于 2013-06-12T13:19:01.013 回答
1

那是因为您已经删除了一个值,但条件 ( customer.size()) 中的初始值保持不变。因此,列表的实际大小是 1,即使一开始是 2 或更多。

我建议你使用迭代器而不是 for 循环:

Iterator it = customers.iterator();
while (iterator.hasNext()) {
    Customer customer= (Customer) it.next();
    //do stuff with the customer

    //remove the customer
    it.remove();
}
于 2013-06-12T13:20:47.643 回答
0

我会说你用所有先前答案给出的建议重写你的方法。像这样的东西:

public void deleteCustomer(){
        String id = null;
        int remember = 0;      //Remembers the deleted index

        id = JOptionPane.showInputDialog(null,"input the id of whome you want to delete",
                "input id", JOptionPane.PLAIN_MESSAGE);

        int id2 = Integer.parseInt(id); //new int id.

       Iterator<Customer> itr = customers.iterator();
        while(itr.hasNext()){
            Customer thisCustomer = itr.next();
            if(id2 == thisCustomer.getID()){
                customers.remove(thisCustomer);
                break;

             }
             remember++;
        }

        if(remember < customers.size()){

            JOptionPane.showConfirmDialog(null,"the customer has been deleted.",
                        "input people", JOptionPane.PLAIN_MESSAGE);

        }
        else{

            JOptionPane.showConfirmDialog(null,"the customer could not be found," +
                    " please check your id",
                            "input people", JOptionPane.PLAIN_MESSAGE);

        }


}
于 2013-06-12T13:34:41.063 回答
0

你的输出有问题。删除索引为 i 的元素后,你记住了 i。

   int i1 = JOptionPane.showConfirmDialog(null,"the customer "
                   + customers.get(remember).getName() + " has been deleted.",
                        "input people", JOptionPane.PLAIN_MESSAGE);

因此,这段代码尝试访问列表中已删除的元素以获取名称。

于 2013-06-12T13:32:54.300 回答