假设我有FooClass类。

public class FooClass {
}

以下行给了我以下编译错误:
// Note I want to create an array of length 4 of Lists of FooClass
List<FooClass> runs[]=new List<FooClass>[4];
Cannot create a generic array of List<FooClass> ...

将不胜感激。

最佳答案

List集合与array不同:

// if you want create a List of FooClass (you can use any List implementation)
List<FooClass> runs = new ArrayList<FooClass>();

// if you want create array of FooClass
FooClass[] runs = new FooClass[4];

UPD:

如果要创建列表数组,则应:
  • 创建数组
  • 使用列表实例
  • 填充此数组

    例子:
    List<FooClass>[] runs = new List[4];
    for (int i = 0; i < runs.length; i++) {
        runs[i] = new ArrayList<>();
    }
    

    09-30 21:27