Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java8如何创建嵌套列表?_Java_Java 8 - Fatal编程技术网

java8如何创建嵌套列表?

java8如何创建嵌套列表?,java,java-8,Java,Java 8,学习和使用Java8。正在尝试创建二维阵列 final List<Integer> row = IntStream.range(0, 3) .boxed() .collect(Collectors.toList()); List<List<Integer>> arr2D = IntStream.range(0, 3)

学习和使用Java8。正在尝试创建二维阵列

final List<Integer> row = IntStream.range(0, 3)
                                   .boxed()
                                   .collect(Collectors.toList());

List<List<Integer>> arr2D = IntStream.range(0, 3)
                                     .map(i -> arr2D.add(i, row)); // will not compile
final List row=IntStream.range(0,3)
.boxed()
.collect(Collectors.toList());
列表arr2D=IntStream.range(0,3)
.map(i->arr2D.add(i,行));//不会编译

如何将行放入二维数组中?这是使用Java8的正确方法吗?

您的问题提到了数组,但您的代码只有列表。如果要生成嵌套列表,请执行以下操作:

List<List<Integer>> arr2D = IntStream.range(0, 3)
                                     .mapToObj(i -> row)
                                     .collect(Collectors.toList());
List arr2D=IntStream.range(0,3)
.mapToObj(i->行)
.collect(Collectors.toList());
当然,使用此代码时,所有内部列表都是相同的(即,相同的实例)。如果希望每个内部列表都是不同的实例:

List<List<Integer>> arr2D = IntStream.range(0, 3)
                                     .mapToObj(i -> new ArrayList<Integer>(row))
                                     .collect(Collectors.toList());
List arr2D=IntStream.range(0,3)
.mapToObj(i->新建阵列列表(行))
.collect(Collectors.toList());
解决方案可以是:

List<List<Integer>> lists = IntStream.range(0, 3)
                                     .mapToObj(i -> IntStream.range(0, 3)
                                                             .mapToObj(j -> j)
                                                             .collect(Collectors.toList()))
                                     .collect(Collectors.toList());
List List=IntStream.range(0,3)
.mapToObj(i->IntStream.range(0,3)
.mapToObj(j->j)
.collect(收集器.toList())
.collect(Collectors.toList());

感谢您的快速回复。不确定我是否在这里做错了什么,您的代码给了我
类型不匹配:无法从List转换为int
错误。@Z-1哦,我的错误。由于要从int映射到引用类型,因此将
map
更改为
mapToObj
@Z-1如果不修改内部列表,则所有内部列表都是同一实例并不重要。但是,如果您不修改它们,并且不介意它们都引用同一个实例,那么为什么需要嵌套列表呢?内部列表实例还不够吗?@Z-1:那么在创建嵌套列表之前,您应该通过包装内部列表,以确保您注意到您的假设(即仅发生读取遍历)是错误的。然后,您根本不需要流操作,只需说
arr2D=Collections.nCopies(3,Collections.unmodifiableList(row))
..@Z-1:不是针对
arr2D
,而是在您的变体中,
也指向不可变列表,这是最好的。如果不需要
变量,也可以内联它:
arr2D=Collections.nCopies(3,Collections.unmodifiableList(IntStream.range(0,3).boxed().collect(Collectors.toList())工作得很好!你能解释一下为什么你使用
mapToObj
而不是
boxed
作为内部循环吗?@Z-1我不知道存在
boxed()
方法。在当前实现中,
boxed()
执行相同的操作:
public final Stream boxed(){return mapToObj(Integer::valueOf);}
;唯一的区别是答案中的代码使用自动装箱。是
的源代码已装箱
?你能分享一下你从哪里得到的吗?