| Author |
question about treeSet (K&B) Chapter 7 Question 3
|
sebastian tortschanoff
Ranch Hand
Joined: Mar 19, 2009
Posts: 68
|
|
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.
|
Power from within.
Failed SCJP 2 times :-(
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
When something is sorted, it is ordered automatically. So you can say that when elements are added to a TreeSet, the set is sorted each time or you can say that the element is inserted at its sorted position...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
sebastian tortschanoff
Ranch Hand
Joined: Mar 19, 2009
Posts: 68
|
|
Yep, thanks Ankit.
Maybe it's the weather here in Germany...
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Coming to your second query, I think you are confused here. Lets take an example of a simple list with the values 1, 2 and 3. Now when you iterate the list, you can image that there is a virtual pointer which points to the next element in the list. So when you use this type of look
Now when you create the iterator for the list, you can imagine that there is a virtual pointer sitting before the first element in the list
When you say hasNext in the loop, it sees that there is an element 1 after the pointer so it returns true. Then when you call it.next(), then 1 is returned and at the same time the virtual pointer moves one step ahead
Then again we call it.hasNext, so again there is an object after the pointer so it returns true and again we call next and the pointer moves one step ahead
Now again it.hasNext is called and it returns true and it.next is called and the pointer moves ahead
Now the call to it.hasNext will return false and the iteration will stop...
|
 |
sebastian tortschanoff
Ranch Hand
Joined: Mar 19, 2009
Posts: 68
|
|
Phew!
Ok. That explains why value 3 is printed.
Thank you very much for your help
Probably i will post some further questions about chapter 7. Actually i'm doing the questions.
See You, and thanks alot.
|
 |
 |
|
|
subject: question about treeSet (K&B) Chapter 7 Question 3
|
|
|