首页 > 开发 > JAVA > 正文

java 泛型的问题

2017-09-07 09:24:03  来源:网友分享

我想写一个方法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]这里有问题,请问怎么解决?谢谢。

解决方案

这个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.

  1. 因为数组和泛型不对付, 所以在不做强制类型转换的情况下, 我们是没有办法得到一个泛型数组的实例的, 编译器会限制此类情况发生(new E[], list.toArray());
  2. 为什么数组和泛型不对付, Effective Java里有例子. 根本原因在于:

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[].

给出例子说明我的理解, 类似的代码, 在泛型集合下, 会在静态编译阶段报错; 而泛型数组在运行阶段给出ArrayStoreException:

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"));}@Testpublic void test111(){    doSomethingToGenArr(new Integer[2]);}@Testpublic void test112(){    doSomethingToGenList(new ArrayList<Integer>());}