2013-06-12 93 views
0

首先,如果刪除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) 
+1

'customers.get(記住).getName()+「已被刪除。」Erm。如何在移除它時期待它在那裏? –

+0

如果Size是1,索引應該是0,那麼調試你的代碼是有一些邏輯問題的...... – aProgrammer

回答

1

不要在遍歷它與一個循環從集合中刪除元素。在大多數情況下,使用迭代器來實現remove方法。

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

或者如果你必須的;遍歷它向後 –

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(); 
} 
0

有你的輸出問題。 刪除索引爲i的元素後,您還記得我。

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

因此,這段代碼嘗試從列表中訪問已刪除的元素以獲取名稱。

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); 

     } 


} 
相關問題