aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class CAst  Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class CAst  Exception" Watch "Class CAst  Exception" New topic
Author

Class CAst Exception

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
It will be great help if some one please explain why the below code throwing Class Cast Exception:

import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}

..
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
A TreeSet requires that either the elements you insert into have to be mutually comparable, i.e. the class definition implements the Comparable interface, or that you create the TreeSet with a Comparator that specifies how two objects in the set will be compared.
ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Thanks Keith ..i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class CAst Exception