gpt4 book ai didi

java - 类型无法创建 List 的通用数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:32 24 4
gpt4 key购买 nike

假设我有 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 Collection 与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];

更新:

如果你想创建列表数组,你应该:

  1. 创建数组
  2. 用 List 实例填充这个数组

例子:

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

关于java - 类型无法创建 List<FooClass> 的通用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15780654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com