| 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: 2341
|
|
|
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.
|
 |
 |
|
|
subject: Class CAst Exception
|
|
|