2014-11-04 36 views
0
public boolean removeAccount (String accountNumber) 
{ 
    int index = 0; 
    boolean found = false; 
    while (index < accounts.size() && !found) 
    { 
     if (accounts.get(index).getAccountNumber().equals(accountNumber)) 
     { 
      found = true; 
      accounts.remove(accountNumber); 
     } 
     else 
      index++; 
    } 
    if (found == true) 
    { 
     return true; 
    } 
    else 
     return false; 
} 

當我進入返回true一個正確的帳號,但它不會從ArrayList中刪除帳戶,任何幫助,將不勝感激:)刪除對象(賬戶)

+0

accounts.remove(指數)...由索引位置刪除對象 – fmodos 2014-11-04 19:33:50

+2

注:塊'while'循環可以用'返回找到替代後;'。 – 2014-11-04 19:35:23

回答

1

two remove methods for ArrayList。一個接受索引,另一個接受對象刪除自己。但是您提供了帳號,而不是索引或帳號本身。該帳號不在該列表中,該帳號是,因此帳號不會被刪除。

你有索引,而不是帳號。

accounts.remove(index); 
+0

謝謝!它現在完美運行! – Patterrz 2014-11-04 19:38:45

0

您使用的參數實際上是List#remove(Object)。您正試圖從列表中刪除String,這絕對不存在。

使用int參數,而不是:

accounts.remove(index);