【发布时间】:2011-09-11 15:45:39
【问题描述】:
我正在迭代一个名为 clientList 的客户端 ArrayList,其中包含来自 Client (user,pass) 类的客户端
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;
}
我正在尝试以下 while (found == false),但如果在 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;
}
}
}
}
【问题讨论】:
-
您还应该考虑使用
HashMap而不是List。键是user,值是pass。然后你的for-loop 就消失了;你会得到if (hashMap.get(user).equals(pass))。但是您必须为此严重更改您的代码...不需要Client类。