简体   繁体   English

只能迭代java.lang.Iterable的数组或实例吗?

[英]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.

相关问题 只能迭代一个数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable 只能在java.lang.Iterable中的数组或实例上进行迭代 - Can only iterate over an array or an instance of java.lang.Iterable in java Eclipse 编译错误显示不正确(只能迭代数组或 java.lang.Iterable 的实例) - Eclipse compile error shown incorrectly (Can only iterate over an array or an instance of java.lang.Iterable) 错误:只能遍历数组或java.lang.Iterable的实例 - Error: can only iterate over an array or an instance of java.lang.Iterable 使用FileUtils时只能迭代数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable while using FileUtils 另一个“只能迭代数组或java.lang.Iterable的实例”问题 - Yet another “Can only iterate over an array or an instance of java.lang.Iterable” issue 规则编译错误:只能迭代java.lang.Iterable的数组或实例 - Rule Compilation error : Can only iterate over an array or an instance of java.lang.Iterable ForStatement-只能迭代数组或java.lang的实例。在csv文件创建中可迭代 - ForStatement - Can only iterate over an array or an instance of java.lang.Iterable in csv file creation 在reducer中的for循环上获得编译错误“只能迭代数组或java.lang.Iterable实例” - Getting compilation error “Can only iterate over an array or an instance of java.lang.Iterable” at for loop in reducer 我正在使用Collection只能迭代数组或java.lang.Iterable的实例 - i am using Collection Can only iterate over an array or an instance of java.lang.Iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM