This is mentioned in the second edition of Effective Java, Item25.
Because of these fundamental differences, arrays and generics do not
mix well. For example, it is illegal to create an array of a generic
type, a parameterized type, or a type parameter. None of these array
creation expressions are legal: new List[], new List[], new
E[]. All will result in generic array creation errors at compile time.
Generally speaking, arrays and generics don’t mix well. If you find
yourself mixing them and getting compile-time errors or warnings, your
first impulse should be to replace the arrays with lists.
Because arrays and generics do not deal with each other, there is no way for us to get an instance of a generic array without forced type conversion. The compiler will limit the occurrence of such situations (new E[], list .toArray());
Why arrays and generics don’t work, there are examples in Effective Java. The fundamental reason is:
Arrays are covariant. This scary-sounding word means simply that if
Sub is a subtype of Super, then the array type Sub[] is a subtype of
Super[].
Give an example to illustrate my understanding. Similar code, under a generic collection, will report an error during the static compilation phase; while a generic array will give ArrayStoreException:
toArray()There are two methods, with parameters and without parameters.
In the case of parameters, the parameters are generic arrays, such as T[] a, which is equivalent to manually constructing an array and passing it into the method. toArray() is internally a copy implementation.
Give two examples.
1:String[][] str_a = (String [][]) arr.toArray(new String[0][0]);
2 :String[][] a = new String[<size>][size]; String [][] str_a = (String [][]) arr.toArray(a);
Of course, the transformation must be successful, otherwise ClassCastException will occur.
The following is the implementation in the source code, which is actually a copy operation.
/**
* Returns an array containing all of the elements in this list
* in proper sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this list. (In other words, this method must allocate
* a new array). The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
* @return an array containing all of the elements in this list in
* proper sequence
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
This is mentioned in the second edition of Effective Java, Item25.
Give an example to illustrate my understanding. Similar code, under a generic collection, will report an error during the static compilation phase; while a generic array will give ArrayStoreException:
during the runtime phase.There is no generic array, that’s why
toArray()
There are two methods, with parameters and without parameters.In the case of parameters, the parameters are generic arrays, such as
T[] a
, which is equivalent to manually constructing an array and passing it into the method.toArray()
is internally a copy implementation.Give two examples.
1:
String[][] str_a = (String [][]) arr.toArray(new String[0][0]);
2 :
String[][] a = new String[<size>][size];
String [][] str_a = (String [][]) arr.toArray(a);
Of course, the transformation must be successful, otherwise ClassCastException will occur.
--------------------------Separating line------------------ ------
The following is the implementation in the source code, which is actually a copy operation.