问题
I'm iterating an ArrayList
of clients named clientList that contains clients from the class Client (user,pass)
ArrayList<Client> clientList= new ArrayList<Client>();
Here's the iteration. I want to stop the iteration if it founds a given user (user) and if the password (pass) matches:
for (Client c : clientList) {
userA = c.getUser();
if (userA.equals(user)) {
passA = c.getPassword();
if (passA.equals(pass)) {
loginOK = true;
found= true;
}
I was trying the following while (found == false) but it's get stucked if it doesnt find an user on the 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;
}
}
}
}
回答1:
I'd write it this way:
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;
}
}
}
}
My guess is that you didn't override equals and hashCode in your Cliente
class or it's not correct.
回答2:
You should break
out of the loop when the value is found.
for (something) {
if (anotherThingIsFound) {
break;
}
}
//Execution will continue here when you break...
Note, that it also is possible to break out of nested loops, with help of labels. E.g.
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
does also work with labels.
回答3:
Why not just break
?
for (Client c : clientList) {
userA = c.getUser();
if (userA.equals(user)) {
passA = c.getPassword();
if (passA.equals(pass)) {
loginOK = true;
found = true;
break;
}
}
}
(I'm assuming you need to tell the difference between getting to the end and finding someone and getting to the end and not finding someone. You probably only need one variable though, rather than two...)
With your while
loop attempt, you're going to iterate over the whole list forever if the user isn't found, and even if the user is found, it will loop over the whole list once - because your for loop is inside the while loop. The while condition is only checked once per iteration of the while loop.
回答4:
You need to use the break
keyword:
for (Client c : clientList) {
userA = c.getUser();
if (userA.equals(user)) {
passA = c.getPassword();
if (passA.equals(pass)) {
loginOK = true;
found = true;
break;
}
See Java break keyword documentation.
回答5:
if you want to use your found
attribute, introduce a break
and remove while loop:
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;
}
That way, you don't need to use the while
loop:
回答6:
Skipped null safety and class cast safety for simplicity
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:
Using break
works fine, but if you wanted to do it with a while loop instead then you could do it like this:
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;
}
}
}
来源:https://stackoverflow.com/questions/6292457/stopping-an-iteration-of-an-arraylist-in-java