E:\practice>javac -Xlint MyArrayList.java
MyArrayList.java:154: warning: [unchecked] unchecked method invocation: <T>smaller(T,T) in MyArrayList is applied to (T,T)
if(list[i] == smaller(list[smallest], list[i]))
^
1 warning
E:\practice>javac TestProg.java
E:\practice>java TestProg
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
at MyListADT.<init>(MyListADT.java:39)
at MyArrayList.<init>(MyArrayList.java:12)
at TestProg.main(TestProg.java:11)
When you do things right, people won't be sure you've done anything at all.
When you do things right, people won't be sure you've done anything at all.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Krep Lock wrote:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
at MyListADT.<init>(MyListADT.java:39)
at MyArrayList.<init>(MyArrayList.java:12)
at TestProg.main(TestProg.java:11)
Istvan Kovacs wrote:The problem is that you create an Object[], which cannot be cast to T[].
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:
Istvan Kovacs wrote:The problem is that you create an Object[], which cannot be cast to T[].
Actually you can (it's a warning after all, not an error), but only if the rest of the code ensures that every interaction with the array respects the T type
That said, the safest choice for the created array type is what T extends. With my stack that is Object, but in Krep's case using "new Comparable[...]" is probably better. After all, you know that every T is a Comparable. Type erasure also turns every occurrence of T into Comparable so why not use Comparable[]?
Istvan Kovacs wrote:Even if you do that, it will fail if the array is passed to the outside world (you cannot cast Comparable[] into String[]).
Rob Prime wrote:It's essential to hide the array from any outside code because otherwise you can cast the T[] to Object[] and then add anything.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:It's essential to hide the array from any outside code because otherwise you can cast the T[] to Object[] and then add anything.
Istvan Kovacs wrote:
Rob Prime wrote:It's essential to hide the array from any outside code because otherwise you can cast the T[] to Object[] and then add anything.
Maybe it's nitpicking, but I don't agree with the underlined reasoning: you cannot "add anything", as the cast will fail.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:Comparable is generic too. The declaration should be That "? super T" is to ensure that T can also be java.sql.Timestamp which is a Comparable<java.util.Date>.
That should solve the warning, but the ClassCastException will still exist. Can you show us the relevant parts of MyListADT? Especially the related fields and its constructor, since that's where the exception is coming from.
Rob Prime wrote:That said, the safest choice for the created array type is what T extends. With my stack that is Object, but in Krep's case using "new Comparable[...]" is probably better. After all, you know that every T is a Comparable. Type erasure also turns every occurrence of T into Comparable so why not use Comparable[]?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Legend has it that if you rub the right tiny ad, a genie comes out.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|