在Eclipse里有如下语句List<String>[] list = new List[5];等号右边出现黄色波浪线,说
Type safety:The expression of type List[] needs unchecked conversion to conform to List<String>[]
不太明白是什么意思,怎样改才能去掉这样警告呢?

解决方案 »

  1.   

    改成List<String>[] list = new List<String>[5];
      

  2.   


    不对啊,这回是红叉叉了。Cannot create a generic array of List<String>
      

  3.   

    范型是类型安全的,这可能是类察除带来的问题
    你可以这样:
    List <List<String>>list = new ArrayList<List<String>>(); 
    不就解决了?
      

  4.   

    恩,在《Thinking in Java》里也看到了类似的解释。
      

  5.   

    List 是接口。不能直接new ;可以改成new List的实现类ArrayList
      

  6.   

    也可以在方法头部加上如下这句:
    @SuppressWarnings("unchecked")
    就ok了,就会经类型安全问题的警告(黄线)取消了。
      

  7.   

    不能直接声明范型数组的,JDK里面现在是不支持范型数组的,如果真的需要数组,可以这样声明一下List<List<String>> list = new ArrayList<List<String>>();
      

  8.   

    没办法改 因为JAVA的泛型是不允许你用正常方式创建数组的(你的创建方式是创建了一堆List<Object>的对象 这也就是为什么会出现黄色警告的原因 是因为类型不匹配 不过没关系 这样用也是可以的)
      

  9.   

    再简单点说
    T[] t = (T[])new Object[size];就是强制把那个T 转成Object的类型我可能说错了 谁纠正下。。
      

  10.   

    Arrays of generics are not type-safe, because arrays (unlike generic collections) are covariant: you can use a String[] when an Object[] is required, but not so with List<String> and List<Object>.Covariant collections are not type-safe unless they are read-only.
    In a little more detail:
    The Java designers decided to disallow arrays of generics because 1) it would not be type-safe, and 2) generics were added to the language precisely to ensure type-safety.Arrays are also not type-safe:String[] strings;
    Object[] objects = strings; // allowed because of covariance
    objects[0] = Long.valueOf(9); // fails at runtime with ArrayStoreExceptionBecause generic types don't carry the type parameter at runtime (that info is "erased"), the JVM cannot even throw an ArrayStoreException in this exampleList<String>[] stringsArray = ... // can't create
    Object[] objects = stringsArray; // allowed because of covariance of arrays
    List<Long> longs = Arrays.asList(Long.valueOf(7));
    objects[0] = longs; // ArrayStoreException cannot be thrown because JVM doesn't know the type parameter Long, it's been erased
    int length = stringsArray[0].get(0).length(); // would throw ClassCastException---type safety compromised
      

  11.   

     一下请问有谁知道:
    http://topic.csdn.net/u/20090309/14/64e0e298-9cfd-4e49-8e6e-71125839a4f3.html
      

  12.   

    用List[]的好处是初始化起来方便,指定了长度,一个循环就行了。如果用List<List<String>> list = new ArrayList<List<String>>();
    要多写一点代码。
      

  13.   

    List[] list=new List[5];
    for(int i=0;i<list.length;i++){
    list[i]=new ArrayList();
    }
    for(int j=0;j<5;j++){
    if('condition'){
    list[j].add('String');
    }
    }如果用列表好像不怎么好实现。
      

  14.   

    上面写得也没说清楚。是这样的。String pars = {"1","2","3,"4","5"};
    List[] list = new List[pars.length];
    for(int i=0; i<list.length; i++) {
      list[i] = new ArrayList();
    }for(int i=0; i<testList.size(); i++) { //这里的testList保存了很多数据,并且是List<Object[]>型的
      Object[] ob = (Object[]) testList.get(i);
      String type = ob[0].toString().trim();
      String par = ob[4].toString().trim();//只需要这个两个数据
      if(testMap.get(type)!=null) {
        for(int j=0; j<pars.length; j++) {
          if(par.equals(pars[j])) {
            list[j].add(type);
          }
        }
      }
    }就是这样初始化的。如果用List<List<String>>,还不知道如何实现。
      

  15.   

    正解,~在jdk源码中到处都是这种强转
      

  16.   

    参考Vector里的
    public synchronized <T> T[] toArray(T[] a) {
            if (a.length < elementCount)
                return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); System.arraycopy(elementData, 0, a, 0, elementCount);        if (a.length > elementCount)
                a[elementCount] = null;        return a;
        }
    自己体会