Results 1 to 4 of 4

Thread: Iterating multiple collections

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved 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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Iterating multiple collections

    Quote 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.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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
  •  



Click Here to Expand Forum to Full Width