java - cannot be cast to [Ljava.lang.Comparable -
so need dynamic ordered list.
public class dynarraylistord<t extends comparable<t>> { private t[] tab ; public dynarraylistord() { tab = (t[])new object[startsize]; } .... main { dynarraylistord tab = new dynarraylistord(); tab.add("john"); tab.add("steve"); }
and when run code error:
exception in thread "main" java.lang.classcastexception: [ljava.lang.object; cannot cast [ljava.lang.comparable; @ structures.dynarraylistord.<init>(dynarraylistord.java:14) @ structures.dynamicarrayappp.main(dynarraylistord.java:119)
the erased type of t[] tab
comparable[]
. thus, need use type in constructor:
public dynarraylistord() { tab = (t[]) new comparable[startsize]; }
you should enable unchecked
warnings avoid these kinds of problems in first place.
Comments
Post a Comment