最初は、配列リスト内の要素がリストの唯一の要素である場合にその要素を削除する際に問題がありましたが、現在は配列リストの最後の位置を削除するときに例外の問題が発生しています。これに対処する最善の方法は何ですか?
編集:振り返ってみると、それが最後の要素であるかどうかを確認し、唯一の場所を保持するためにダミー要素を入れるとうまくいきます。
コード:
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)