我想写一个方法list->array 想利用泛型,代码如下
public static <T> T[] list2Array(List<T> list){
T[] array = (T[])list.toArray(new T[list.size()]);
return array;
}
也就是传过来的String就返回String数组 Integer就返回Integer数组。但是这个new T[list.size]这里有问题,请问怎么解决?谢谢。
PHP中文网2017-04-17 11:24:00
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.
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:
during the runtime phase.private <T> void doSomethingToGenArr(T[] tArr){
Object[] oArr = tArr;
oArr[0] = new String("aaa");
}
private <T> void doSomethingToGenList(List<T> list){
List<Object> l1 = list; /* compile error */
l1.set(0, new String("aaa"));
}
@Test
public void test111(){
doSomethingToGenArr(new Integer[2]);
}
@Test
public void test112(){
doSomethingToGenList(new ArrayList<Integer>());
}
天蓬老师2017-04-17 11:24:00
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.
/**
* 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);
}