Hi Halli, Got it! Thanks very much!
- Hiran On Mon, Sep 14, 2009 at 3:29 AM, Hallgrímur Njálsson < [email protected]> wrote: > It is possible to have those 3 types of Object in the same TreeSet. > Strings, MyOwnClass and Integers. > > ts.add(new Integer(1).toString()) is not the way! > > Take a better look at the TreeSet constructor... > - Halli > > > 2009/9/13 Hiran Dias <[email protected]> > > >> HI, >> >> Is it possible to do the homework like this: >> >> ts.add(new Integer(1).toString()); >> >> >> Not sure whether it is agree to the homeworks expectation of "Add Integer >> Object". >> >> >> Thanks & Regards, >> Hiran >> >> >> >> >> >> On Sat, Sep 12, 2009 at 7:28 PM, Deyan Pavlov <[email protected]>wrote: >> >>> *thanks for the idea, it seems to work.* >>> ** >>> *about Integer class, I haven't been able to add it to the tree. I guess >>> for the time being this homework will have to wait.* >>> >>> >>> ----- Original Message ----- >>> *From:* Hallgrímur Njálsson <[email protected]> >>> *To:* [email protected] >>> *Sent:* Saturday, September 12, 2009 3:54 PM >>> *Subject:* Re: [java programming] Java Collections Framework - lab 1008; >>> TreeSet >>> >>> >>> I have figured out this part of the homework... >>> >>> You have to write your "MyOwnClass" so it implements Comparable >>> >>> I did it like this... >>> >>> class MyOwnClass extends Object implements Comparable { >>> . >>> . >>> . >>> >>> Then you have to implement the method compareTo so it can compare >>> MyOwnClass object to the calling object >>> >>> >>> I include my compareTo method... >>> >>> public int compareTo(Object o) { >>> return (this.toString().compareTo(o.toString())); >>> } >>> >>> Hope this will help you. >>> >>> I am still having problem comparing the Integer object to the String and >>> MyOwnClass Objects... Have you figured that out yet? >>> >>> My regards >>> -Halli >>> >>> >>> 2009/9/12 Deyan Pavlov <[email protected]> >>> >>> *if you mean:* >>>> ** >>>> ** >>>> * TreeSet<Object> ts = new TreeSet<Object>();* >>>> >>>> * ts.add("one") ; >>>> ts.add("two") ;* >>>> >>>> * MyOwnClass obj1 = new MyOwnClass( ); >>>> >>>> ts.add(obj1) ;* >>>> ** >>>> *this still has the same error *ClassCastException >>>> >>>> *Maybe another idea?* >>>> >>>> >>>> ----- Original Message ----- >>>> *From:* Zahari Palazov <[email protected]> >>>> *To:* [email protected] >>>> *Cc:* [email protected] >>>> *Sent:* Friday, September 11, 2009 7:26 PM >>>> *Subject:* Re: [java programming] Re: Java Collections Framework - lab >>>> 1008; TreeSet >>>> >>>> *TreeSet<Object> ts = new TreeSet<Object>(); >>>> try with this >>>> * >>>> On Fri, Sep 11, 2009 at 6:48 PM, Deyan Pavlov >>>> <[email protected]>wrote: >>>> >>>>> >>>>> Hi, >>>>> I understand Treeset is a collection of elements that are comparable. >>>>> But the homework is to put in this collection String, Integer and >>>>> MyOwnClass >>>>> objects, so three very different types. >>>>> >>>>> I think that so far there hasn't been any idea in previous lessons >>>>> about how >>>>> to put all these things together in one group. >>>>> >>>>> So far I have solved all homeworks completely on my own but now this >>>>> seems >>>>> to be a bit out of boundary exception for me. >>>>> >>>>> Any practical idea how to put these objects together? >>>>> >>>>> Thanks in advance!! >>>>> >>>>> >>>>> ----- Original Message ----- >>>>> From: "Ashok A V" <[email protected]> >>>>> To: <[email protected]> >>>>> Cc: "Free Java Programming Online Training Course By Sang Shin" >>>>> <[email protected]> >>>>> Sent: Friday, September 11, 2009 9:45 AM >>>>> Subject: Re: [java programming] Java Collections Framework - lab 1008; >>>>> TreeSet >>>>> >>>>> >>>>> 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 -~----------~----~----~----~------~----~------~--~---
