C
Colin Hemmings
Hi there I was wondering if anyone can help me using the contains method
for arratlists.
Basically, I have a class 'User' with attibutes 'userName, name, email'
I then have an Arraylist 'users' contains several users.
I have a method on another class which takes in a userName and returns
'true' if a user with that userName is in the 'users' list. The method
is below:
public boolean userExist(String userName)
{
boolean valid = false;
String temp = null;
for (int x=0; x<(users.size()); x++)
{
temp = ((User)users.get(x)).getUserName();
if (userName.equals(temp))
{
valid = true;
}
}
return valid;
}
This method works fine but I was wondering if I could do the same check
in fewer lines using the 'contains()' method. I have tried the following:
public boolean userExist(String userName)
{
boolean valid = false;
String temp = null;
valid = users.contains(userName);
return valid;
}
But although this compiles it doesnt work.
Would anybody be able to offer any suggestions?
for arratlists.
Basically, I have a class 'User' with attibutes 'userName, name, email'
I then have an Arraylist 'users' contains several users.
I have a method on another class which takes in a userName and returns
'true' if a user with that userName is in the 'users' list. The method
is below:
public boolean userExist(String userName)
{
boolean valid = false;
String temp = null;
for (int x=0; x<(users.size()); x++)
{
temp = ((User)users.get(x)).getUserName();
if (userName.equals(temp))
{
valid = true;
}
}
return valid;
}
This method works fine but I was wondering if I could do the same check
in fewer lines using the 'contains()' method. I have tried the following:
public boolean userExist(String userName)
{
boolean valid = false;
String temp = null;
valid = users.contains(userName);
return valid;
}
But although this compiles it doesnt work.
Would anybody be able to offer any suggestions?