Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
JAVA中带有Lambda表达式的For循环_Java_Foreach_Lambda_Java 8 - Fatal编程技术网

JAVA中带有Lambda表达式的For循环

JAVA中带有Lambda表达式的For循环,java,foreach,lambda,java-8,Java,Foreach,Lambda,Java 8,为什么当我使用下面的代码时我会得到IndexOutOfBoundsException 代码: List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList()); System.out.print("the list: "); ints.forEach((i) -> { System.out.print(ints.get(i-1) + " ");

为什么当我使用下面的代码时我会得到IndexOutOfBoundsException

代码:

    List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());
    System.out.print("the list: ");
    ints.forEach((i) -> {
        System.out.print(ints.get(i-1) + " ");
    });
    List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
    System.out.print("the list: ");
    ints.forEach((i) -> {
        System.out.print(ints.get(i-1) + " ");
    });
但当我把清单改成一位数时,一切都很好

代码:

    List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());
    System.out.print("the list: ");
    ints.forEach((i) -> {
        System.out.print(ints.get(i-1) + " ");
    });
    List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
    System.out.print("the list: ");
    ints.forEach((i) -> {
        System.out.print(ints.get(i-1) + " ");
    });

对Iterable的每个元素执行给定的操作,直到所有 元素已被处理或操作引发异常

所以我就进去了


是列表中的每个元素。如果这些元素中减去1的任何一个大于或等于列表大小的5,则尝试获取超出范围的元素。

我认为原因非常清楚

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});
大致翻译为:

for (Integer i : ints) {
    System.out.println(ints.get(i - 1) + " ");
}
这将导致IndexOutofBoundsException,因为我引用了每个列表的元素,这些元素中的每一个-1都会给出一个明显超出范围的索引。对于第一个示例,我将是21,它给出了21-1==20的索引,这超出了您创建的列表的范围

例如:

List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());
所以当你运行这个:

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});
ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});
计算机获取第一个元素并尝试执行lambda的主体:

Execute System.out.print(ints.get(i-1) + " ");:
    First element is 21
    21 - 1 == 20
    ints.get(20) --> IndexOutOfBoundsException
Execute System.out.print(ints.get(i-1) + " ");:
    First element is 2
    2 - 1 == 1
    ints.get(1) --> 8
    Print 8
Execute System.out.print(ints.get(i-1) + " ");:
    Second element is 8
    8 - 1 == 7
    ints.get(7) --> IndexOutOfBoundsException
第二个例子是:

List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
所以当你运行这个:

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});
ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});
计算机遍历元素并尝试执行lambda的主体:

Execute System.out.print(ints.get(i-1) + " ");:
    First element is 21
    21 - 1 == 20
    ints.get(20) --> IndexOutOfBoundsException
Execute System.out.print(ints.get(i-1) + " ");:
    First element is 2
    2 - 1 == 1
    ints.get(1) --> 8
    Print 8
Execute System.out.print(ints.get(i-1) + " ");:
    Second element is 8
    8 - 1 == 7
    ints.get(7) --> IndexOutOfBoundsException
显然,第二个示例中的代码不是您实际拥有的代码。我怀疑您实际拥有的代码是:

List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
    System.out.print(i + " ");
                     ^^^^^^^ <-- this is different
});

这与你发布的内容完全不同。

你做得太过分了。你想要的是

List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
    System.out.print(i + " ");
});
forEach中的i不是循环计数器,而是项目本身。因此,我将采用值2,8,7,4,3,对于第二次迭代,ints.get 8-1将超出范围。

更简单:

String s = Stream.of(21,22,32,42,52)
                 .collect(Collectors.joining(" "));

有点不对劲,因为这两个片段都会在我的计算机上导致IndexOutOfBoundsException。lambda中的我是什么?ints.得到了什么。。是吗?@SotiriosDelimanolis我不确定,因为我玩游戏是为了弄清楚它们,但据我所知,我是函数,返回是int.getfunction@KickButtowski将lambda转换为常规循环,您将看到why@user3580294我怎么看?为什么?@KickButtowski这只是一个巧合,因为你的列表中元素的值…@KickButtowski你不明白我在说什么。您将获取列表中的元素并将其用作索引。巧合的是,这个特定列表中的元素恰好是有效的索引,正如公认的答案所指出的,一旦元素太大,就会出现越界异常。@KickButtowski在你的问题中两段代码都试过了吗?因为这两个都会抛出异常。上一个问题中的代码恰好是正确的,但正如公认的答案所指出的,错误的值将抛出exceptions@kick如果你有一个[1,2,3,4]的列表,你从中减去一个,你得到[0,1,2,3]。如果将这些作为第一个列表的索引使用,则会得到值[1、2、3、4]。但是如果你有一个列表[7,8,9,10],你从每个列表中减去一个,那么[6,7,8,9],然后用它们作为索引,你会得到一个超出范围的索引,因为在[7,8,9,10]中索引7处没有元素。@KickButtowski是的,你应该理解我们刚才在答案中已经解释的内容。lambda中的i是基础列表中的每个连续元素。当我在线程main java.lang.RuntimeException:不可编译的源代码中运行时引发此异常-错误的符号类型:java.util.stream.stream.collect at addnewlinetotxtfile.addnewlinetotxtfile.mainAddNewLinetoTxtFile.java:30 java结果:1您能告诉我吗知道为什么吗?这是一个与NetBeans相关的问题,请清理并重建您的项目。还有谷歌搜索不可编译的源代码——错误的符号类型——你会得到关于解决方法的进一步建议。我喜欢这样简单的干净的黑客。敬礼