aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about treeSet (K&B) Chapter 7 Question 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "question about treeSet (K&B) Chapter 7 Question 3" Watch "question about treeSet (K&B) Chapter 7 Question 3" New topic
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
    
    2

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
    
    2

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: question about treeSet (K&B) Chapter 7 Question 3
 
Similar Threads
Why it returns a new iterator object
Result of this code?
mock exam example
NullPointerException in TreeSet
Implementing java.util.Iterator