2014-11-04 54 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);