Why do i get classcast exception for this code

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){}
}

basically classcast exception is thrown when the class can not be casted with the proper form that is demanded.
1> in case of set the value should be mutually comparable. but unfortunately in you code you have not implemented comparable interface. you should implement that interface.
2> after using the comparable interface you should implement the method <class object 1>.compateTo(<class object 2>)
the above two process should exclude the exception from you code hopefully.
check out!

Similar Messages

Maybe you are looking for

  • Graphics card

    I've recently upgraded my graphics card, which works fine. It has two mini display ports and one dvi. I've decided i want to run a dual display setup using my old monitor. Can I reinstall my old card and run the vga monitor off that? I daren't instal

  • DefaultFont File Opens in Textedit on Boot?

    Can't figure out why this is happening!  OSX 10.8.2 on a 2008 Macbook Pro.  Reset PRAM, restored ML directly from Apple, ran permissions and disk verification with DU... At the end of every boot process, TextEdit opens a file called "DefaultFont-lock

  • How to register created fonts in Java 5?

    Java 6 provides the registerFont(Font) method in GraphicsEnvironment to register fonts created using Font's createFont(int, InputStream) method. However, I need my application to work using Java 5. I looked in the Java 6 source code hoping to find th

  • KXML Error

    Hello i need some help here, i dont know what to do anymore. I'm using eclise and i'm trying to run my KXML Parser and i'm getting this error: Running with storage root C:\Documents and Settings\daniel\j2mewtk\2.5.2\appdb\DefaultColorPhone Running wi

  • Why is my start up disk full

    I have 120 gb and i know there is no way I used it all up yet.