I have an ArrayList
that contains some object, such as User
, and each object has a name
and password
property. How can I
Another thought: If User
class can be uniquely defined by the username and if you override equals
with something like:
public boolean equals(Object arg0) {
return this.name.equals(((user) arg0).name);
}
You can remove the User
without iterating through the list . You can just do :
list.remove(new User("John Doe"))
Just search through the ArrayList of objects you get from the user, and test for a name equal to the name you want to remove. Then remove that object from the list.
Your code might look like this:
List<User> users = new ArrayList<User>();
public void removeUser(String name){
for(User user:users){
if(user.name.equals(name)){
users.remove(user);
}
}
}