Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Reading Containers in Depth chapter from the Thinking In Java book. Saw this example(modified from SortedMapDemo.java):

Java
TreeSet<String> sortedSet = new TreeSet<String>();
Collections.addAll(sortedSet,
          "a b c d e f g h"
            .split(" "));

...
Java
Iterator<String> it = sortedSet.iterator();
for(int i = 0; i <= 6; i++) {
if(i == 3) low = it.next();
if(i == 6) high = it.next();
else it.next();
}
print(low);
print(high);


What baffles me is that while 'low' prints out "d", 'high' prints out "h", for which I think should be "g" instead.
If we're calling it.next() in each iteration, shouldn't the 7th iteration gives "g"??
Posted
Updated 16-Jun-14 23:10pm
v4

1 solution

Solved. Sorry guys.
It's because the

if;
if ;
else;

structure, when i ==3, .next() gets called twice
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900