why is this exception not thrown when I use the get() method()?
There are two reasons for this :
- Assuming that no elements were added to
array
in your program, the array
is going to be initialized with sz
number of null
values.. The statement (T)array[index]
is trying to cast a null
into a T
(where T
essentially gets replace by Object
through type erasure). A null
can be cast into any type so this is not an issue.
- Assuming that some elements are added to
array
in your program, the only way that the statement (T)array[index]
will work without any issues is if the element at position index
is of the same type or a subtype used for instantiating Array
(e.g Array<Number> arr = new Array<Number>
and arr[index]
is 1. This won't be an issue; however, try inserting a String
at arr[index]
and you are bound to get a ClassCastException
from the get
method as well)
Why does the compiler say that (T[])array is unchecked cast, and then ClassCastException is thrown when I use the rep()
The only way for you to get a ClassCastException
is that the type represented by T
is not the same as the type of elements that array
can hold. For example, doing something as follows will result in a ClassCastException
:
Array<Integer> a = new Array<Integer>();
Integer[] intArray = a.rep();//ClassCastException
Here, T[]
represents an Integer[]
so there are no issues in compiling the code; however, at runtime, array
is of type Object[]
. An array of Object
cannot be downcast to an array of Integer
. It would also help to know that arrays are dynamically created by the JVM unlike other classes.
One way to fix this would be to instantiate Array
as Array<Object> arr = new Array<Object>
.