高手指点!!

解决方案 »

  1.   

    ArrayList[] al=new ArrayList[3];
      al[0]=new ArrayList<String>();
      al[1]=new ArrayList<String>();
      al[2]=new ArrayList<String>();
      

  2.   

    ArrayList<String>[] lists = new ArrayList<String>[4];
    for(int i = 0; i < lists.length; i++) {
       lists[i] = new ArrayList<String>();
    }
      

  3.   

    如果1楼那样,还不如
    ArrayList<String>[] al=new ArrayList[3];这样更干脆,反正都有警告
      

  4.   

    List<String> list= new ArrayList<String>();
      

  5.   

    When we work with generic types, the runtime check for array store exceptions is no longer sufficient because a check performed at runtime does not have access to the compile-time type parameter information. Consider this (hypothetical) code:List<String>[] wordlists = new ArrayList<String>[10];
    ArrayList<Integer> ali = new ArrayList<Integer>();
    ali.add(123);
    Object[] objs = wordlists;
    objs[0] = ali;                       // No ArrayStoreException
    String s = wordlists[0].get(0);      // ClassCastException!
    If the code above were allowed, the runtime array store check would succeed: without compile-time type parameters, the code simply stores an ArrayList into an ArrayList[] array, which is perfectly legal. Since the compiler can't prevent you from defeating type safety in this way, it instead prevents you from creating any array of parameterized type. The scenario above can never occur because the compiler will refuse to compile the first line.
    -------
    摘自《Java技术手册》第五版 - 4.1.2.4 "Arrays of parameterized type"
      

  6.   

    感谢楼上提供 又找了更全的4.1.2.4 Arrays of generic type 
    4.1.2.4 关于数组的参数化类型 Arrays require special consideration when working with generic types. Recall that an array of type S[ ] is also of type T[], if T is a superclass (or interface) of S. Because of this, the Java interpreter must perform a runtime check every time you store an object in an array to ensure that the runtime type of the object and of the array are compatible. For example, the following code fails this runtime check and throws an ArrayStoreException: 当数组遇到泛类型时,我们需要特别的考虑。回想一个类型 S[] 的数组也是类型为 T[] 的数组,如果 T 是 S 的超类(或是接口)。因此,必须每当你存储一个对象到一个数组时,Java 的解析器就会执行一个运行期检查,以确保运行时放入的对象的类型和数组的类型相容。例如,下面的代码在运行期检查是失败的,并且抛出一个 ArrayStoreException 异常: 
    Java代码 
    String[] words = new String[10];   
    Object[] objs = words;   
    objs[0] = 1;  // 1 autoboxed to an Integer, throws ArrayStoreException    
               // 1 被自动装箱到 Integer,抛出ArrayStoreException 异常   
     Although the compile-time type of objs is Object[], its runtime type is String[ ], and it is not legal to store an Integer in it. 虽然编译时 objs 的类型是 Object[],但在运行时的类型是 String[] ,并非法的把 Integer 值放入其中。 When we work with generic types, the runtime check for array store exceptions is no longer sufficient because a check performed at runtime does not have access to the compile-time type parameter information. Consider this (hypothetical) code: 当我们应用泛类型时,运行期关于数组存储异常的检测已经不能满足需要,因为一个运行期的检测不能读取编译期的泛型参数信息。考虑下面的代码(假设可以执行): 
    Java代码 
    List<String>[] wordlists = new ArrayList<String>[10];   
    ArrayList<Integer> ali = new ArrayList<Integer>();   
    ali.add(123);   
    Object[] objs = wordlists;   
    objs[0] = ali;                       // No ArrayStoreException //没有 ArrayStoreException 异常   
    String s = wordlists[0].get(0);         // ClassCastException!  //ClassCastException 异常   
       
     If the code above were allowed, the runtime array store check would succeed: without compile-time type parameters, the code simply stores an ArrayList into an ArrayList[] array, which is perfectly legal. Since the compiler can't prevent you from defeating type safety in this way, it instead prevents you from creating any array of generic type. The scenario above can never occur because the compiler will refuse to compile the first line. 如果上面的代码可以执行,运行时数组存储检查将是成功的;没有编译时的泛型参数,这段代码简单的把一个 ArrayList 存到 ArrayList[] 数组中,那都是非常正确的。因为编译器不能从这个方式阻止你破坏类型安全,它只能阻止你创建任何的参数化类型类型数组。其实上面的一幕是不会发生的,因为编译器将会拒绝第一个行的编译。 Note that this is not a blanket restriction on using arrays with generics; it is just a restriction on creating arrays of parameterized type. We'll return to this issue when we look at how to write generic methods. 注意这不是完全禁止数组应用泛型;这仅是禁止用参数化类型创建数组。当看过怎样写泛型方法后,会回到这个话题。 详见
    http://www.javaeye.com/post/141517
      

  7.   

    总结下来不能这么做的原因就是这个了
    a check performed at runtime does not have access to the compile-time type parameter information
    无法在运行期获取编译期的类型参数信息
      

  8.   

    http://topic.csdn.net/u/20071213/13/cec88600-5000-4d78-a41f-29fffd411b72.html以前这贴中讨论过,请看最后一篇我的回复。to 火龙果:貌似你在那贴里面也有参与,怎么忘了?
      

  9.   

    呵呵,我记忆力很差的,除非印象特别深刻否则我根本就记不住的。[size=30px]o(∩_∩)o[/size]不过这个我记住了。我要把那个帖和这个帖加入网摘~~