删除arraylist异常中的最后一个元素

时间:2013-06-12 13:16:23

标签: java exception-handling arraylist

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

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

代码:

  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 个答案:

答案 0 :(得分:2)

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

Iterator<Customer> it = customers.iterator();
while(it.hasNext()) {
 if(it.next().getId() == id2) {
  it.remove();
 }
}

答案 1 :(得分:1)

那是因为您已经删除了一个值,但条件(customer.size())中的初始值保持不变。因此,列表的ACTUAL大小为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();
}

答案 2 :(得分:0)

输出中存在问题。 删除索引为i的元素后,请记住i。

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

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

答案 3 :(得分: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);

        }


}