Hello folks,
i have a simple question about (K&B) Chapter 7 Question 3 a bit modified.
The issue is the answer.
Here the code:
the ouput:
0
1
2
3
6
9
The answer says:
"...At runtime the TreeSet will try to sort the elements as they are added..."
What means this? The TreeSet will be ordered and sorted by the way the elements are added?
Or do it mean: The TreeSet is ordering and sorting the elements every time a new element is added? <-- I think this is true!
Also i have a question about the output of the code above.
look here:
The TreeSet has 6 Elements, ordered and sorted (0,1,2,3,6,9).
hasNext() returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)
But when the
Iterator it has come to the element wich holds the value 9, will hasNext() return true? If not why is element with value 9 printed though? I'm asking, cause element with value 9 has no next element.
Can anyone answer this, please?
Sincerely yours
---------Update
I think the last element (with value 9) hast no next and therefor th e while-loop breaks.
But number 9 is already printed, cause element with value 6 has a next element.