This discussion is archived
2 Replies Latest reply: Aug 13, 2009 8:04 AM by 807580 RSS

How do you print items from an array list that fit a certain condition?

807580 Newbie
Currently Being Moderated
I have an array list full of people. People can be set to single or married, is there anyway I can just display the people from the arraylist that are married? I've been reading through alot of stuff about arraylists and there doesnt seem to be anything on printing things depending on context.
  • 1. Re: How do you print items from an array list that fit a certain condition?
    807580 Newbie
    Currently Being Moderated
    thebigfish wrote:
    I have an array list full of people. People can be set to single or married, is there anyway I can just display the people from the arraylist that are married? I've been reading through alot of stuff about arraylists and there doesnt seem to be anything on printing things depending on context.
    Have you tried simply iterating over the list, checking for your condition, and printing out the person's details?
    for ( Person p : yourListOfPeople ) {
        if ( p.isMarried() ) {    // Assuming isMarried returns a boolean value determining if the person is married
            System.out.println( p );  // Assuming you've overriden toString
        }
    }
  • 2. Re: How do you print items from an array list that fit a certain condition?
    807580 Newbie
    Currently Being Moderated
    Yep that worked when I adapted it to mine, thanks alot. I dont know why I cant work these things out my self, I guess when you are concentrating to much you lose sight of the simple things.

    thanks