Results 1 to 12 of 12
- 06-27-2011, 07:40 PM #1
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:
As always, any help would be greatly appreciated.Java Code:if(nodesUsed.contains(each of the nodes in the ArrayList<Node<Adjustment>>)) { //then we do this } else { //we do something else }
Thank You.>> What can be asserted without proof can be dismissed without proof. <<
- 06-27-2011, 07:53 PM #2
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!
- 06-27-2011, 08:00 PM #3
Hello KevinWorkman
You mean like this:
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 conditionJava Code:for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x] { if(nodesUsed.contains(n.getName()) { // Do Code00 } }
I only want Code00 to run if ALL the Elements in the Array meet the condition.
Thanks>> What can be asserted without proof can be dismissed without proof. <<
- 06-27-2011, 08:06 PM #4
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.
- 06-27-2011, 08:13 PM #5
Hi Dark
I hadn't thought of a while loop to be honest. Do mean like so:
In the above, wouldn't Code00 run even if only one of the elements meets the condition ?Java Code:for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x] { while(nodesUsed.contains(n)) { // Do Code00 } }
Thanks, Wish you a good night sleep
Last edited by Ciwan; 06-27-2011 at 08:25 PM.
>> What can be asserted without proof can be dismissed without proof. <<
- 06-27-2011, 08:17 PM #6
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!
- 06-27-2011, 08:19 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-27-2011, 08:46 PM #8
Based on what Kevin and JosAH said, my code looks like this now:
Have I messed up again ? or it correct this time ? It certainly seems OK when I run it through my head.Java Code:for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x] { while(!nodesUsed.contains(n)) { break; } // Do Code00 }>> What can be asserted without proof can be dismissed without proof. <<
- 06-27-2011, 08:51 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 06-28-2011, 02:40 AM #10
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.
- 06-28-2011, 10:57 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 06-28-2011, 11:08 AM #12
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
-
Can anyone help out to meet this criteria
By Manojx in forum New To JavaReplies: 3Last Post: 03-13-2011, 03:51 PM -
Can anyone help out to meet this criteria
By Manojx in forum New To JavaReplies: 7Last Post: 03-12-2011, 04:58 PM -
Meet the code please
By Cylab in forum New To JavaReplies: 9Last Post: 08-12-2010, 04:14 PM -
Meet and Interact with Top Recruiters and Recruiting companies of USA.
By esurvey in forum Jobs OfferedReplies: 0Last Post: 09-02-2009, 03:28 PM -
Meet and Interact with Top Recruiters and Recruiting companies of USA.
By esurvey in forum Jobs DiscussionReplies: 0Last Post: 08-31-2009, 09:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks