|
-
Dec 12th, 2005, 06:14 AM
#1
Thread Starter
Fanatic Member
Iterating multiple collections
How can i iterate through 2 collections at the same time?
Could i do it with just one iterator?
Both collections are the same size.
p.s. Can you use example code if possible?
Thanks.
Last edited by x-ice; Feb 25th, 2007 at 07:05 PM.
-
Dec 12th, 2005, 07:45 AM
#2
Re: Iterating multiple collections
What is the purpose of this iteration?
If you are just comparing two collection:
Code:
public class Smaple {
public static void main(String args[]) {
int collection1[] = {1,2,3,4,5}
, collection2[] = {1,2,3,4,5}
, x = 0;
for (int i = 0; i < collection1.length; i++) {
if (collection1[i] == collection2[i]) {
x++;
}
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Dec 12th, 2005, 09:43 AM
#3
Thread Starter
Fanatic Member
Re: Iterating multiple collections
 Originally Posted by ComputerJy
What is the purpose of this iteration?
I need to execute the print method of each object in both collections in a specific order. Like this:
Collection1[1].print()
Collection2[1].print()
Collection1[2].print()
Collection2[2].print()
Collection1[3].print()
Collection2[3].print()
Collection1[4].print()
Collection2[4].print()
and so on...
To be more specific
I have a dating system (uni coursework) that is meant to match male and female customers. I am doing this the simple (gaining slightly lower marks) way.
I have two collections :- maleCust and femaleCust, both contain Customer objects.
I want to match the first male with the first female found in both collections, and so on... I think an iterator would be perfect for this.
Last edited by x-ice; Dec 12th, 2005 at 09:51 AM.
-
Dec 12th, 2005, 01:39 PM
#4
Re: Iterating multiple collections
put it in a nested for loop
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|