Results 1 to 12 of 12
  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default Checking that all elements in collection meet a condition

    Hello Java Friends.

    I have an ArrayList of things (Nodes<Adjustment> to be precise). And I want to check that every single one of the elements in the ArrayList, meet a certain condition. If condition is met by all, then I want to return True and do something. If one or two don't meet the condition then I want to return a False and not do the thing.

    I am not really sure whether there is a technical name for what I'm trying to achieve. But here's some pseudo-code for what I'm trying to do:

    Java Code:
    if(nodesUsed.contains(each of the nodes in the ArrayList<Node<Adjustment>>))
    {
         //then we do this
    } else {
         //we do something else
    }
    As always, any help would be greatly appreciated.

    Thank You.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,015
    Rep Power
    10

    Default

    You're going to have to loop through the ArrayList (or use an Iterator) and check each element against whatever condition you want.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hello KevinWorkman

    You mean like this:

    Java Code:
    for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x]
    {
         if(nodesUsed.contains(n.getName())
         {
              // Do Code00
         }
    }
    Cause that is what I thought initially, but then I realised that { Code00 } will run even if only one of the elements in the Array met the condition

    I only want Code00 to run if ALL the Elements in the Array meet the condition.

    Thanks

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    What about a while loop? While its true it will continue going through your code, if the boolean ever becomes false or if you reach the end of your array list using an iterator you can insert a break statement. Then check outside of the loop if your boolean is true, and if it is then run code00.

    I'm a bit tired, and this has a chance of being completely retarded, so I would verify with someone else that this is a good idea. It makes sense in my head, but I have no idea if it is displayed properly. Anyways I'm off, I need sleep. I hope this helped.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hi Dark

    I hadn't thought of a while loop to be honest. Do mean like so:

    Java Code:
    for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x]
    {
    	while(nodesUsed.contains(n))
    	{
    		// Do Code00
    	}
    }
    In the above, wouldn't Code00 run even if only one of the elements meets the condition ?

    Thanks, Wish you a good night sleep
    Last edited by Ciwan; 06-27-2011 at 09:25 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,015
    Rep Power
    10

    Default

    How would you do this by hand, or with a piece of paper and a pencil?

    You'd look look through the list, and if you saw an element that did NOT meet the condition, you'd stop looking through the list and not do anything else. You'd know that all of the elements met the condition once you reached the end of the list.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ciwan View Post
    I hadn't thought of a while loop to be honest. Do mean like so:
    No, not like so; more like so:

    Java Code:
    boolean allSatisfied= true;
    for (T element : collection) 
       if (!condition(element)) {
          allSatisfied= false;
          break;
       }
    if (allSatisfied)
       // Do Code00
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Based on what Kevin and JosAH said, my code looks like this now:

    Java Code:
    for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x]
    {
    	while(!nodesUsed.contains(n))
    	{
    		break;
    	}
    
    	// Do Code00
    }
    Have I messed up again ? or it correct this time ? It certainly seems OK when I run it through my head.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ciwan View Post
    Based on what Kevin and JosAH said, my code looks like this now:

    Java Code:
    for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x]
    {
    	while(!nodesUsed.contains(n))
    	{
    		break;
    	}
    
    	// Do Code00
    }
    Have I messed up again ? or it correct this time ? It certainly seems OK when I run it through my head.
    I never said anything like that and I'm sure Kevin did neither. Reread Kevin's and my reply again (carefully) and try again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Use a method that returns a boolean. As soon as you find an element that does not meet the requirement return false. If you get to the end of the method all elements must meet the requirement so return true.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    Use a method that returns a boolean. As soon as you find an element that does not meet the requirement return false. If you get to the end of the method all elements must meet the requirement so return true.
    This here.
    You have a requirement that essentially says "if all elements in node pass this test then do this".
    Your method would answer the "do all elements in node pass this test" part.

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    JosAH said to loop through your code, in the for loop he provided you would then check your conditions. If any condition that must be met was not met then you would set the boolean variable to false, break out of the loop and continue to the following if statement that would only run if your boolean was true. If the loop finished you would simply continue down to the if statement and do code00 would run.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Can anyone help out to meet this criteria
    By Manojx in forum New To Java
    Replies: 3
    Last Post: 03-13-2011, 04:51 PM
  2. Can anyone help out to meet this criteria
    By Manojx in forum New To Java
    Replies: 7
    Last Post: 03-12-2011, 05:58 PM
  3. Meet the code please
    By Cylab in forum New To Java
    Replies: 9
    Last Post: 08-12-2010, 05:14 PM
  4. Replies: 0
    Last Post: 09-02-2009, 04:28 PM
  5. Replies: 0
    Last Post: 08-31-2009, 10:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •