Heba:
You need to override the toString method for your Objects in your
Object Class, for example something like the following:

  @Override
     public String toString() {
        String a = this.name + " is " + Integer.toString(this.age) + "
years of age.";
        return a;
    }

where name represents the name in your Object and age represents the
age stored in your object.

Jeffrey



On Nov 28, 8:26 pm, Heba Samy <[EMAIL PROTECTED]> wrote:
> Hey All,
> I have a problem wz the homework of lab 1016 (Collections Framework)
> It throws exceptions when i create a TreeSet and LinkedHashSet from the array 
> of diffrent abject types(Integer, String,object of MyOwnClass ) bec. TreeSet 
> and LinkedHashSet sorts the elements inside them so when trying to do so, 
> they cant compare diffrent objects(integer and String forexample)!!!
> thats how i understood the problem and it may be wrong...
>
> So, here is the exception it throws:
> Me
> 1
> 2
> [EMAIL PROTECTED]
> 3
> You
> [EMAIL PROTECTED]
> Exception in thread "main" java.lang.ClassCastException: 
> mycollectionproject.MyOwnClass cannot be cast to java.lang.Comparable
>         at java.util.TreeMap.put(TreeMap.java:542)
>         at java.util.TreeSet.add(TreeSet.java:238)
>         at mycollectionproject.UtilityClass.Display(UtilityClass.java:18)
>         at mycollectionproject.Main.main(Main.java:58)
> Java Result: 1
> As Shown it works well with just the HashSet then throws the exceptions..
>
> And Thats also the code if u wanna check it:
> -----------------------------------------------------------------------------------
> UtilityClass
> --------------------------------------------------------------------------------
> package mycollectionproject;
> import java.util.Set;
> import java.util.Iterator;
>
> public class UtilityClass {
>     public static void Display(Set s, Object[] objects){
>         for (int i = 0 ; i < objects.length ; i++){
>             s.add(objects[i]);
>         }
>         for(Iterator it = s.iterator();it.hasNext();){
>             System.out.println(it.next());
>         }
>     }
>
> }
>
> --------------------------------------------------------
> the Main
> ------------------------------------------------
> package mycollectionproject;
> import java.util.HashSet;
> import java.util.Set;
> import java.util.TreeSet;
> import java.util.Iterator;
> import java.util.LinkedHashSet;
>
> class MyOwnClass{
>     String name = " ";
>     int age = 0;
>     public MyOwnClass(String name,int age){
>         this.name = name;
>         this.age = age;
>     }}
>
> public class Main {
>  public static void main(String[] args) {
>         // Create HashSet Object
>         String me = new String ("Me");
>         String u = new String("You");
>         MyOwnClass o1 = new MyOwnClass("Heba",21);
>         MyOwnClass o2 = new MyOwnClass("Mostafa",18);
>         Integer one = new Integer(1);
>         Integer two = new Integer(2);
>         Integer three = new Integer(3);
>         Object objects[] = {
>             me,
>             u,
>             o1,
>             o2,
>             one,
>             two,
>             three
>         };
> Set s = new HashSet();
>         UtilityClass.Display(s,objects);
>         s = new TreeSet();
>         UtilityClass.Display(s,objects);
>         s = new LinkedHashSet();
>         UtilityClass.Display(s,objects);
>  }
>
> }
>
> So, how can i solve this????
> Any Help would be Appreciated
> Thanks in advance,
> Heba

--~--~---------~--~----~------------~-------~--~----~
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