2011-06-09 52 views
0

停止一个ArrayList的迭代我遍历客户的ArrayList命名客户端列表包含客户从类Client (user,pass)在Java中

ArrayList<Client> clientList= new ArrayList<Client>(); 

这里的迭代。我想停止迭代,如果它开创一个给定的用户(用户),如果密码(口令)匹配:

  for (Client c : clientList) { 
       userA = c.getUser(); 
       if (userA.equals(user)) { 
        passA = c.getPassword(); 
        if (passA.equals(pass)) { 
         loginOK = true; 
         found= true; 
        } 

我尝试以下,而(发现== FALSE),但它得到stucked如果它没有找到该ArrayList的用户:

 while (found == false) {/
      for (Client c : clientList) { 
       userA = c.getUser(); 
       if (userA.equals(user)) { 
        passA = c.getPassword(); 
        if (passA.equals(pass)) { 
         loginOK = true; 
         found= true; 
        } 
       } 
      } 
     } 
+4

use keyword:'break;' – oliholz 2011-06-09 12:04:45

+0

你也应该考虑使用'HashMap'而不是'List'。关键是'用户'和'传递'的价值。然后你的'for'循环消失;你可以得到'if(hashMap.get(user).equals(pass))'。但是你必须严格改变你的代码......不需要'Client'类。 – toto2 2011-06-09 13:26:00

回答

1

我会写这样说:

while (!found) { 
    for (Cliente c : clientList) { 
     userA = c.getUser(); 
     if (userA.equals(user)) { 
      passA = c.getPassword(); 
      if (passA.equals(pass)) { 
       loginOK = true; 
       found= true; 
       break; 
      } 
     } 
    } 
} 

我的猜测是,你没有重载equals和hashCode在Cliente类或它不是正确的。

+0

加上'break','found'不再需要 – oliholz 2011-06-09 12:08:35

+0

我不会接受我的回答。看到其他人都有赞成票,我认为接受其他人是正义的。 – duffymo 2011-06-09 12:42:12

8

你应该break圈外的当值被发现。

for (something) { 
    if (anotherThingIsFound) { 
     break; 
    } 
} 
//Execution will continue here when you break... 

注意,它也有可能爆发的嵌套循环,带标签的帮助。例如。

outer: 
while (someCondition) { 
    for (criteria) { 
     if (somethingHappened) { 
     break outer; 
     } 
     if (anotherThingHashHappened) { 
     break; 
     } 
    } 
    //Execution will continue here if we execute the "normal" break. 
} 
//Execution will continue here when we execute "break outer;" 

continue也可以使用标签。

5

为什么不只是break

for (Client c : clientList) { 
    userA = c.getUser(); 
    if (userA.equals(user)) { 
     passA = c.getPassword(); 
     if (passA.equals(pass)) { 
      loginOK = true; 
      found = true; 
      break; 
     } 
    } 
} 

(我假设你需要告诉让到底,找到别人和获得结束找人的区别,你可能只需要一个变量虽然,而不是两个... ...)

与您while循环的尝试,你会在整个列表迭代永远如果用户没有找到,并且即使用户发现,它将循环整个过列出一次 - 因为你的循环是里面的 while循环。 while循环仅在while循环的每个迭代中检查一次。

3

您需要使用break关键字:

 for (Client c : clientList) { 
      userA = c.getUser(); 
      if (userA.equals(user)) { 
       passA = c.getPassword(); 
       if (passA.equals(pass)) { 
        loginOK = true; 
        found = true; 
        break; 
       } 

Java break keyword documentation

2

,如果你想用你的found属性,引入break和删除while循环:

for (Cliente c : clientList) { 
    userA = c.getUser(); 
    if (userA.equals(user)) { 
     passA = c.getPassword(); 
     if (passA.equals(pass)) { 
     loginOK = true; 
     found= true; 
     } 
    } 

    if (found) 
     break; 
} 

这样,你不需要使用while循环:

+0

+1 - 这是正确的答案。 – duffymo 2011-06-09 12:26:49

1

跳过空安全和为简单起见

public class Cliente { 
    public boolean equals(Object other){ 
     Cliente cOther = (Cliente) other; 
     return this.getUser().equals(other.getUser()) && 
       this.getPassword().equals(other.getPassword()) 
    } 
    public int hashCode(){ 
     return this.getUser().hashCode()*this.getPassword().hashCode(); 
    } 
} 

... 
Cliente c = new Cliente(); 
c.setPassword(pass); 
c.setUser(user); 
boolean found = clientList.contains(c); 
1

使用break类转换安全工作正常,但如果你想d你可以这样做:

Iterator<Client> it = clientList.iterator(); 
    while (it.hasNext() && !found) { 
     Client c = it.next(); 
     userA = c.getUser(); 
     if (userA.equals(user)) { 
      passA = c.getPassword(); 
      if (passA.equals(pass)) { 
       loginOK = true; 
       found = true; 
      } 
     } 
    }