Results 1 to 2 of 2

Thread: Iterations

  1. #1
    Join Date
    Oct 2003
    Posts
    29

    Unhappy Unanswered: Iterations

    I have two iterations. I'm not sure if this is correct, but what I'm trying to do is go through one of them, when I find a match, I do a merge. But if they don't match, I still want to loop through the second iteration. If they two id's that I'm matching never match, then I want to move to the next record of the first iteration. What is the best way to do this? Currently I have this:

    ListIterator cpIterator = completionProfiles.listIterator();
    ListIterator tasksIterator = tasks.listIterator();

    //Merge Tasks with CompletionProfiles
    try {
    CompletionProfile currentCP = (CompletionProfile)cpIterator.next();
    while(tasksIterator.hasNext()) {
    Task currentTask = (Task)tasksIterator.next();
    if(currentTask.equals(currentCP)) {
    if(currentCP.isActiveInd()) {
    currentTask.mergeWith(currentCP);
    } else {
    tasksIterator.remove();
    }
    currentCP = (CompletionProfile)cpIterator.next();
    }

    }
    } catch(NoSuchElementException nsee) {
    //No further merging necessary.
    //Merging complete.
    }
    return true;

    Any help would be appreciated.

    Thanks,
    Bailee

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    You need to use two loops.
    Code:
    for (Iterator iter1 = collection1.iterator(); iter1.hasNext();) {
        Object obj1 = iter1.next();
    
        for (Iterator iter2 = collection2.iterator(); iter2.hasNext();) {
            Object obj2 = iter2.next();
    
            if (some condition) {
                //do something;
            }
        }
    }
    Depending on what type of collection you are using, you should also be able to use:
    Code:
    collection1.retainAll(collection2);
    That will make collection1 the intersection of the 2 collections.
    Give a man a fish and you feed him for a day. Teach a man to fish and you lose a steady customer.
    -----------
    Eclectic Web Development (under heavy construction)

Posting Permissions

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