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

标签 java exception 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);

            }



        }

the error

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)

最佳答案

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

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

关于java - 删除 arraylist 异常中的最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17066551/

相关文章:

ajax - JSF ajax 请求中的异常处理

java - 我可以帮忙将一些错误消息转化为有形信息吗?

java - 为什么 ArrayList 有一个 clone 方法

java - JScrollPane 的问题——尝试在模型更改时更新它

javascript - 有没有办法使用 javascript 和 Angular 或任何其他 javascript 从可编辑的 pdf 中获取数据

java - 通用方法 T 扩展

java - 从 JTable 填充 JTextField 时出错

c# - BLL 返回字符串或 DTO

java - Java中从url读取CSV文件

java - 从保存在 ArrayList 中的对象输出字符串值