Results 1 to 6 of 6
  1. #1
    Warmonger is offline Member
    Join Date
    Jan 2011
    Posts
    16
    Rep Power
    0

    Default Set iterator doesn't work

    So, the code is simple:

    Java Code:
            for (Iterator<Monster> it = monsters.iterator(); it.hasNext(); )
            {
                Monster m = it.next();
                Vec2d v = new Vec2d(game.playerC.x, game.playerC.y);
                v.subVec(new Vec2d(m.x, m.y));
                v.unitVec();
                v.rescale(m.movement.mag());
    
                m.movement = v;
            }
    However, does not work.

    The set is declared as follows inside this class:
    Java Code:
        Set<Character> characters = Collections.synchronizedSet(new TreeSet<Character>());
    Nothing happens. However, when I use HashSet instead, it acts only for one element and ignores next ones.
    I use trivial hash function for that, maybe it's the reason:
    Java Code:
        public int compareTo(Object o)
        {
            return 0;
        }
    Still, all I need is unordered collection with random access.
    Your feedback would be appreciated.

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Try using a while loop instead, because I'm fairly sure for loops don't work like that in Java. Here are Java's tutorials on For Loops:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The For-Each Loop

    Java Code:
    Iterator<Monster> it = monsters.iterator();
    while(it.hasNext())
    {
        Monster m = it.next();
        Vec2d v = new Vec2d(game.playerC.x, game.playerC.y);
        v.subVec(new Vec2d(m.x, m.y));
        v.unitVec();
        v.rescale(m.movement.mag());
    
        m.movement = v;
     }

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Warmonger View Post
    I use trivial hash function for that, maybe it's the reason:
    Java Code:
        public int compareTo(Object o)
        {
            return 0;
        }
    So all objects are considered equal? While I sympathize with the idea, most certainly and a isn't a b and your set will only store one value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Warmonger is offline Member
    Join Date
    Jan 2011
    Posts
    16
    Rep Power
    0

    Default

    Nope, that's not it.

    I used default hashCode for Object class in comparison and it worked.
    Java Code:
        public int compareTo(Object o)
        {
            if (hashCode() > o.hashCode())
                return 1;
            else
                return -1;
        }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    TreeSet uses compareTo to determine whether something is equal, so mucking up compareTo such that it does not give the same results as equals() for example...well, you get what you deserve. It's all there in the docs after all.

    As for HashSet, have you mucked up the hashcode method as well?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Warmonger View Post
    Nope, that's not it.

    I used default hashCode for Object class in comparison and it worked.
    Java Code:
        public int compareTo(Object o)
        {
            if (hashCode() > o.hashCode())
                return 1;
            else
                return -1;
        }
    Yes, your previous implementation of the compareTo( ... ) method was faulty and your current implementation doesn't make sense if you haven't overridden the hashCode() method and even then it is faulty. Read the API documentation for the Comparable<T> interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Iterator
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 12:17 PM
  2. for..iterator
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 02:12 PM
  3. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  4. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM
  5. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM

Posting Permissions

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