[英]Can only iterate over an array or an instance of java.lang.Iterable?
I try to forEach a 2d-list, that put a int [] array to lambda. 我尝试forEach一个2d列表,将int []数组放入lambda。 The compiler complains "Can only iterate over an array or an instance of java.lang.Iterable"
编译器抱怨“只能迭代java.lang.Iterable的数组或实例”
List<int []> list2d = new ArrayList<>();
list2d.add(new int[] {1,3,5,7});
list2d.add(new int[] {2,4,6,8});
list.forEach((array)-> { */// why here array can't be iterated?*
for(int num: array) {
System.out.println(num);
}
});
I tried it on my computer and it works but you need to use the correct variable in: 我在计算机上尝试了它,但是它可以工作,但是您需要在以下位置使用正确的变量:
list.forEach((array)-> { // why here array can't be iterated?
for(int num: array) {
System.out.println(num);
}
});
you must iterate list2d not list, isn't it? 您必须迭代list2d而不是list,不是吗? Also, remember the comments in java are between /* */ or after // if is one line.
另外,请记住java中的注释在/ * * /之间,或者//如果在一行之后。
If I understood you correctly, you want to iterate over elements inside the array elements. 如果我对您的理解正确,那么您想遍历数组元素内的元素。 You could achieve this with something like the following:
您可以通过以下方式实现此目的:
list2d.stream()
.flatMapToInt( Arrays::stream )
.forEach( System.out::println );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.