java.lang.ClassCastException: cannot be cast to java.lang.Integer -
i wanted create compareto
method gives me error. can me ?
package defaultpackage; public class huffmanentry implements entry<integer, string> { private string c; private integer freq; public huffmanentry(string c, integer freq){ this.c = c; this.freq = freq; } @override public int compareto(integer arg0) { if(getkey()<arg0){ return -1; }else if(getkey()>arg0){ return 1; }else{ return 0; } } @override public integer getkey() { return freq; }
here's error:
exception in thread "main" java.lang.classcastexception: defaultpackage.huffmanentry cannot cast java.lang.integer @ defaultpackage.huffmanentry.compareto(huffmanentry.java:1)
there serious problems quality of "evidence" have provided us:
the stacktrace seems exception thrown @ line 1 of
huffmanentry.java
, , impossible.the stacktrace seems code attempting cast
huffmanentry
objectinteger
incompareto
, (according source code) cannot happen in method. objects used in method have declared type ofinteger
, , there no explicity typecasts.the stacktrace shows
compareto
1 , method call, , impossible.
(3 impossible things before lunch? nah! don't buy it!)
in short, evidence have shown not real. has been tweaked / mangled, , in ways make impossible interpret.
please provide real (complete , unedited) stacktrace, , source code precisely matches compiled code ran produce stacktrace. compilation errors.
alternatively, provide sscce can run ourselves reproduce facially impossible behaviour / evidence.
looking @ code linked to, think understand general problem ... if not specific one.
basically, mixing generic , non-generic code , (you must be) ignoring resulting compiler warnings unchecked conversions, use of raw types , on. result errors should picked java compile-time type checker not being reported.
for example, in myheap.java
, write:
private arraylist<entry> entrylist;
but entry
generic interface, meaning arraylist being interpreted arraylist<entry<object, object>>
object
raw type corresponding unbounded type parameters k
, t
. there int compareto(k)
method in entry
interface ... , raw type signature int compareto(object)
.
now @
if (entrylist.get(i*2).compareto(entrylist.get(i*2+1))==-1){
note calling entry.compareto(object)
passing entry
argument. compiler has said ok .... because thinks using raw types @ point. if had been using generics properly, compiler have realized calling entry<k, v>.compareto(entry<k, v>)
generic method signature entry<k, v>.compareto(v)
. have been flagged compilation error.
i suspect explains strange exception too. java compiler has added hidden runtime typecasts make sure jvm's core runtime type safety not compromised. seeing exception when 1 of runtime checks fails. , happens because ignored or suppressed compile time checks should have told of programming error.
anyway, bottom line should not ignore compiler warnings misuse of generics, unchecked conversions, etc. @ peril ... , in case, got bitten.
Comments
Post a Comment