Thanks Ashok for this Informations. I am trying to solve homework 2.4 in Lab 1008 but I am stuck, and there is something that I must be misunderstanding.
According to the homework description the TreeSet is supposed to have 2 String objects, 2 MyOwnClass objects and 3 Integer objects. In your answer to Deyan you state: "So if you plan to use TreeSet see to that you add the same kind of objects which are sortable by a compareTo() method" I have implemented MyOwnClass so it is comparable To String and I can add both Strings and MyOwnClass to the TreeSet and get it displayed sorted on the console. But the Integer object is not comparableTo String only To Integer and extending the Integer object to overwrite the compareTo method is not possible becose Integer is a final class. Also the String object is not comparable to Integers ... Given that the above is correct understanding, the only way to get the homework done is to write MyOwnInteger class which is comparable To String ?? My understanding is that the homework is not done until "Someone else should be able to open and run it." so handing it in with an exceptions errors is not an option! In hope that someone could clarify this for me - Halli 2009/9/11 Ashok A V <[email protected]> > > Hi , > > TreeSet is the Set which actually sorts your added items. > So now the confusion happens when you add objects of different types. > Say first you add two Strings : > > ts.add("one") ; > ts.add("two") ; > > Then you add three Integers : > > ts.add(new Integer(1)) ; > ts.add(new Integer(2)) ; > ts.add(new Integer(3)) ; > > And finally you add MyOwnClass object to the set > > MyOwnClass obj1 = new MyOwnClass( ); > ts.add(obj1) ; > > Now remember Tree Set is a collection that sorts the objects in your > Set collection. > > public TreeSet() > > Constructs a new, empty set, sorted according to the elements' natural > order. All elements inserted into the set must implement the > Comparable interface. Furthermore, all such elements must be mutually > comparable: e1.compareTo(e2) must not throw a ClassCastException for > any elements e1 and e2 in the set. If the user attempts to add an > element to the set that violates this constraint (for example, the > user attempts to add a string element to a set whose elements are > integers), the add(Object) call will throw a ClassCastException. > > So if you added only string then String Class implements Comparable > interface which has a compareTo() method. So Sorting of strings is > possible.The problem occurs when it comes to the integer objects , the > string class is not able to cast it and it fails saying that : > > Exception in thread "main" java.lang.ClassCastException: > java.lang.String cannot be cast to java.lang.Integer > > So if you plan to use TreeSet see to that you add the same kind of > objects which are sortable by a compareTo() method > > For more reading : > http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html > > Thanks, > Ashok A V > > On Fri, Sep 11, 2009 at 1:17 AM, Deyan Pavlov <[email protected]> > wrote: > > 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!! > > > > > > > > > -- > Victory belongs to the most persevering. > - Napoleon > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
