Lab 1008, 2.4 homework - TreeSet iterator:

import java.util.TreeSet;
import java.util.Iterator;
import java.util.*;
 
public class Main {
    
         
    public static void main(String[] args) {
        // TODO code application logic here
        TreeSet ts = new TreeSet();
         ts.add("one") ;
         ts.add("two") ;

// HERE comes the problem, trying to add  the Integer(1), (2) and (3)
 
          ts.add(new Integer(1)) ;
          ts.add(new Integer(2)) ;
          ts.add(new Integer(3)) ;
 
           MyOwnClass obj1 = new MyOwnClass( );
           ts.add(obj1) ;

    }
}

Result is:
Exception in thread "main" java.lang.ClassCastException: java.lang.String 
cannot be cast to java.lang.Integer
        at java.lang.Integer.compareTo(Integer.java:35)
        at java.util.TreeMap.put(TreeMap.java:545)
        at java.util.TreeSet.add(TreeSet.java:238)
        at mytreeset.Main.main(Main.java:21)
Java Result: 1

-------------------------------------------------------------------------------------------
 
 MyOwnClass obj1 = new MyOwnClass( );
           ts.add(obj1) ;

If I add the two rows above, adding the MyOwnClass obj1, which is properly 
created in another file, 
 then comes an even longer list of errors that didn't happen when applying this 
same method ADD() in LinkedList, ArrayList and HashSet

Exception in thread "main" java.lang.NoClassDefFoundError: mytreeset/MyOwnClass
        at mytreeset.Main.main(Main.java:16)
Caused by: java.lang.ClassNotFoundException: mytreeset.MyOwnClass
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)


 

Why can't I add objects to the TreeSet, while I could add them with not 
exceptions in the case of LinkedList, ArrayList and HashSet ?

Thank you!!
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to