起初,如果删除数组列表中的元素是列表中唯一的元素,则会出现问题,但现在在删除数组列表的最后一个位置时出现异常问题。处理这个问题的最佳方法是什么?
编辑:回顾一下,检查它是否是最后一个元素并放入一个虚拟元素来保持唯一的位置会起作用。
编码:
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)