Results 1 to 8 of 8
Thread: why this ClassCastExceptioni????
- 03-01-2011, 09:40 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 27
- Rep Power
- 0
why this ClassCastExceptioni????
In following code why it gives class cast exception??? If i comment insesrting second person object then it is ok... What is the reason ..!
Thank you..
import java.util.*;
public class Testy{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10)); //If i comment this then OK
System.out.println(s);
}
}
class Person{
int x;
Person(int i){
}
}
- 03-01-2011, 10:25 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What does the compareTo method of your Person class look like?
- 03-01-2011, 10:26 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 27
- Rep Power
- 0
There is not compareTo method for this...
BUT i can still add element 1... but not 2..
- 03-01-2011, 10:27 AM #4
First of all, post code in tags to make it more readable.
Read the API for Set
some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException.
and for TreeSet
a TreeSet instance performs all element comparisons using its compareTo (or compare) method
You need to have your Person class implement Comparable.Better than that, use Generics, that's what it's meant for.Java Code:import java.util.Set; import java.util.TreeSet; public class Testy { 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 implements Comparable { int x; Person(int i) { x = i; } public int compareTo(Object o) { return x - ((Person) o).x; } }dbJava Code:import java.util.Set; import java.util.TreeSet; public class Testy { public static void main(String a[]) { Set s = new TreeSet<Person>(); s.add(new Person(20)); s.add(new Person(10)); System.out.println(s); } } class Person implements Comparable<Person> { int x; Person(int i) { x = i; } public int compareTo(Person o) { return x - o.x; } }
- 03-01-2011, 10:29 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Last edited by masijade; 03-01-2011 at 11:16 AM. Reason: Typo "will" not "wall" ;-)
- 03-01-2011, 10:40 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
All sorted structures (like your TreeSet) would require a default sort behaviour. That's why you need the compareTo() method implemented.
- 03-01-2011, 10:52 AM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
TreeSet's job is to sort data. If you were adding some Integer or String type it'd have been fine and there is already a mechanism to sort. But this object is your own object and TreeSet doesn't know how to sort it. So you have to implement Comparable
- 03-01-2011, 10:54 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
A TreeSet needs to compare objects a and b where a < b or a > b or a == b. It either does that by using a Comparator or the objects to be stored in the set have to implement the Comparable<T> interface where T is the type of the objects to be stored. Your Person class doesn't implement that interface nor did you supply a Comparator implementation.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks