【问题标题】:Stopping an iteration of an ArrayList in Java在 Java 中停止 ArrayList 的迭代
【发布时间】: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 类。

标签: java arraylist iteration


【解决方案1】:

找到值后,您应该将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 也适用于标签。

【讨论】:

    【解决方案2】:

    为什么不只是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 循环尝试,如果找不到用户,您将永远遍历整个列表,即使找到用户,它也会循环遍历整个列表一次 - 因为你的 for 循环是 inside 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

      【讨论】:

        【解决方案4】:

        如果您想使用 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 循环:

        【讨论】:

          【解决方案5】:

          我会这样写:

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

          我的猜测是您没有在 Cliente 类中覆盖 equals 和 hashCode 或者它不正确。

          【讨论】:

          • 不再需要break, found
          • 我不会接受我的回答。看到其他人有赞成票,我认为接受其他人是公平的。
          【解决方案6】:

          为简单起见,跳过了 null 安全性和类强制转换安全性

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

          【讨论】:

            【解决方案7】:

            使用break 可以正常工作,但如果您想使用 while 循环来代替,那么您可以这样做:

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

            【讨论】:

              猜你喜欢
              • 2011-08-31
              • 1970-01-01
              • 2015-12-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-12-07
              • 2021-09-26
              相关资源
              最近更新 更多