Stopping an iteration of an ArrayList in Java
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;
}
开发者_JAVA技巧 }
}
}
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.
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.
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.
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:
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.
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);
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;
}
}
}
精彩评论