Saturday, August 21, 2010

TreeSet: Counting.java

Courtesy of Sun Web Learning Center

5. import java.util.*;
6. class Counting {
7.   public static void main(String [] args) {
8.     TreeSet<String> t = new TreeSet<String>();
9.     if(t.add("one "))
10.       if(t.add("two "))
11.         if(t.add("three "))
12.           t.add("four ");
13.     for(String s : t)
14.       System.out.print(s);
15.   }
16. }

What is the result?


A     one
B     one three two
C     one two three
D     one two three four
E     four one three two
F     Compilation fails.

E is correct. The TreeSet is sorted alphabetically.

No comments:

Post a Comment