1

I need help writing a for each loop which searches through an array list called peoplelist of type people. The loop needs to search for the values String postcode and String name in the array. It then needs to return their ID if it is found, and null if it is not. Any sort of help would be great!

5
  • 1
    it looks like you don't want to find "two elements" in an array list, but rather "two attributes" of all the People elements in an array list. Correct? Could you let us know how your People class look like? Commented Mar 22, 2011 at 11:33
  • Yeah I mean attributes sorry. Will get the code up give me 2 secs Commented Mar 22, 2011 at 11:39
  • also, do you want to return the first instance of People whose postcode and name match the ones you're searching for, or do you want to return all instances of People whose postcode and name match the ones you're searching for? Commented Mar 22, 2011 at 11:43
  • All instances. Although it's highly unlikely that both people will have the same name and postcode. Commented Mar 22, 2011 at 11:46
  • @Jimmy: Is it? I once had a case in a database application where two (unrelated) people lived at the same address, had the same name and the same birthday! Commented Mar 22, 2011 at 12:12

6 Answers 6

3

If the class People is written like a Java bean (i.e. with standard getter methods), something like this would do the job:

for (People person : peopleList) {
  if (person.getName().equals(name) && person.getPostcode().equals(postCode))
    return person.getId();
}
return null;

If a person's name or postcode can be null, you may want to flip the equals calls to avoid null pointer exceptions (e.g. name.equals(person.getName()) instead of person.getName().equals(name)).

Btw Person would be a better name.

Sign up to request clarification or add additional context in comments.

Comments

2

Need to make a lot of assumptions about your classes, but something like this should suffice:

for (People person : peoplelist) {
    if (person.getPostCode().equals(postcode) && person.getName().equals(name)) {
        return person.getId();
    }
}
// deal with not being found here - throw exception perhaps?

Comments

1

With “two elements”, do you mean “two attributes of some class”? If so, something along these lines would do:

String id = null;
for(People p : peoplelist) {
    if(somePostcode.equals(p.postcode) && someName.equals(p.name)) {
        id = p.id;
        break; // no need to continue iterating, since result has been found
    }
}
// result “id” is still null if the person was not found

Comments

0
//In case multiple persons match :)
List<String> result = new LinkedList<String>();

for (People person : peopleList) {
  if (person.getName().equals(name) && person.getPostcode().equals(postCode))
    result.add(person.getId()); 
}

if(result.isEmpty()){
  return null;
}else{
  return result;
}

Comments

0
People foundPerson;
for (People eachPeople : peoplelist )
{
    if (Integer.valueOf(eachPeople.getID()) == 10054 
         && "Jimmy".equals(eachPeople.getName()))
    {
         foundPerson= eachPeople;  
         break;
    } 
}

Comments

0

Assuming you have a Person bean, then if you want to retrieve all instances of Person whose postcode and name match some values, you may do something like this:

public List<Person> searchFirst(List<Person> persons, String postcode, String name) {
    List<Person> matchingPersons = new ArrayList<Person>();
    for (Person person : persons) {
        if (person.getPostcode().equals(postcode) && person.getName().equals(name))
            matchingPersons.add(person);
    }
    return matchingPersons;
}

Next time, you may want to show us your code, so we can help you in understanding what you're doing wrong :)

1 Comment

Ok sorry I'm new to this site, I would like to just say I did attempt this but it's my first time using for-each loops and I can't find any simple examples of them to help me understand :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.