Results 1 to 2 of 2
  1. #1
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default Iterating through HashMap problem

    I have code that looks like this:

    Java Code:
    fileOut.print( iterator.next() );
    currentPattern=(ChoicePattern)playerProfiles.get(iterator.next());
    The problem is as you'll notice the iterator calls the next() method twice when I only want to call it once, so the Player Profile calls the next profile instead of the current one which is what I want.

    Any ideas?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    can't you first call iterator.next() and store the result in a variable, then just use that variable as the parameter of the two method calls?

    i.e.,
    Java Code:
    Object myObj = iterator.next(); // you'll need to use the correct "type" 
                                     // here and cast if not using generics
    fileOut.print(myObj);
    currentPattern=(ChoicePattern)playerProfiles.get(myObj);
    Last edited by Fubarable; 01-28-2009 at 11:35 PM.

Similar Threads

  1. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:34 PM
  2. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:53 AM
  3. Iterating through ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:50 AM
  4. Iterating through result set in JSTL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:31 AM

Posting Permissions

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