Infinite for loop

hi

I have the following for loop:

for (TreeMap <String, Integer> i = result; i.size()!=0; i.headMap (i.lastKey()))

{out.println("keyword = " + i.lastKey() + " ; page no. = " + i.get(i.lastKey()));

result is a tree map that contains several strings (the keys) and integers (values).

I need to note that in the process of creating result some values (i.e. strings) were repeated and therefore their values were replaced (run over).

When I run the program it just perform the loop again and again without getting rid of the last value / key pair in i in every iteration.

Can anyone explain why this is?

thanks in advance.

[682 byte] By [razleva] at [2007-10-3 2:19:12]
# 1
Please re-post using the code formatting button. You'll get more/quicker replies that way.
Dick_Adamsa at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 2

hi

I have the following for loop:

for (TreeMap <String, Integer> i = result; i.size()!=0; i.headMap (i.lastKey()))

{out.println("keyword = " + i.lastKey() + " ; page no. = " + i.get(i.lastKey()));

result is a tree map that contains several strings (the keys) and integers (values).

I need to note that in the process of creating result some values (i.e. strings) were repeated and therefore their values were replaced (run over).

When I run the program it just perform the loop again and again without getting rid of the last value / key pair in i in every iteration.

Can anyone explain why this is?

thanks in advance.

razleva at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 3
At first glance it looks like a problem with your ending condition. What prints out?
zadoka at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 4
elimination is the last key and its value is 267.it prints out "keyword = elimination ; page number = 267" over and over (until i terminate it)Message was edited by: razlev
razleva at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 5
How does the size get reduced to zero? I see nothing that reduces it!P.S. I hate your code layout.
sabre150a at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 6
a. sorry about the layout. I am kind of new to this. b. shouldn't: i.headMap (i.lastKey())"throw away" the last key / value pair every time?
razleva at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 7
I think your third statement in the "for" should be:i = i.headMap(i.lastKey());That is:for (TreeMap <String, Integer> i = result;i.size()!=0;i = i.headMap (i.lastKey())) // Change
MLRona at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...
# 8
Thanks a lot, I've been agonizing about this for a good few hours!
razleva at 2007-7-14 19:18:06 > top of Java-index,Java Essentials,New To Java...