Results 1 to 6 of 6
Thread: Set iterator doesn't work
- 05-31-2011, 09:48 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 16
- Rep Power
- 0
Set iterator doesn't work
So, the code is simple:
However, does not work.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; }
The set is declared as follows inside this class:
Nothing happens. However, when I use HashSet instead, it acts only for one element and ignores next ones.Java Code:Set<Character> characters = Collections.synchronizedSet(new TreeSet<Character>());
I use trivial hash function for that, maybe it's the reason:
Still, all I need is unordered collection with random access.Java Code:public int compareTo(Object o) { return 0; }
Your feedback would be appreciated.
- 05-31-2011, 10:03 AM #2
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; }
- 05-31-2011, 10:24 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-31-2011, 10:32 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 16
- Rep Power
- 0
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; }
- 05-31-2011, 10:50 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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?
- 05-31-2011, 11:39 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Iterator
By Dayanand in forum New To JavaReplies: 2Last Post: 03-10-2011, 12:17 PM -
for..iterator
By jon80 in forum New To JavaReplies: 2Last Post: 11-28-2010, 02:12 PM -
Iterator as return and argument or Iterator-String(not visible in the test file)
By Aldarius in forum New To JavaReplies: 0Last Post: 05-18-2010, 12:53 AM -
Iterator help
By alpdog14 in forum New To JavaReplies: 2Last Post: 10-13-2009, 08:42 PM -
iterator
By venkatallu in forum Advanced JavaReplies: 3Last Post: 09-23-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks