在JAVA中使用Lambda表达式进行循环 [英] For Loop with Lambda Expression in JAVA

查看:2978
本文介绍了在JAVA中使用Lambda表达式进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我使用以下代码时,我得到 IndexOutOfBoundsException



代码:

 列表<整数> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList()); 
System.out.print(list:); ((i) - > {
System.out.print(ints.get(i-1)+);
});
ints.forEach

我的错误堆栈:

at java.util.ArrayList.get(ArrayList.java:414)
在Agent.lambda $ main $ 1(Agent.java:33)
在Agent $$ Lambda $ 8 / 980546781.accept(Unknown Source)
at java.util.ArrayList.forEach(ArrayList.java:1234)
at Agent.main(Agent.java:32)
列表:Java结果:1

但是当我将列表更改为一位数字时,一切正常

Code:

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

输出:

  2 8 7 4 3 


解决方案

认为原因很明显。

pre $ $ $ $ $ $ ints.forEach((i) - > {
System.out。 print(ints.get(i-1)+);
});

大致转换为:

  for(Integer i:ints){
System.out.println(ints.get(i - 1)+);

$ / code>

这将导致 IndexOutOfBoundsException i 是指每个列表的元素,并且这些元素中的每一个 - 1都会给出一个清晰的索引 / em>越界。对于你的第一个例子, i 将会是 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)+);
});

计算机取第一个元素,并尝试执行lambda:

 执行System.out.print(ints.get(i-1)+);:
第一个元素是21
21 - 1 == 20
ints.get(20) - > IndexOutOfBoundsException

第二个例子:

 列表与LT;整数> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList()); 

变成

  ints == [2,8,7,4,3] 

运行这个:

  ints.forEach((i) - > {
System.out.print(ints。 get(i-1)+);
});

计算机通过元素并尝试执行lambda:

 执行System.out.print(ints.get(i-1)+);:
第一个元素是2
2 - 1 == 1
ints.get(1) - > 8
打印8
执行System.out.print(ints.get(i-1)+);:
第二个元素是8
8 - 1 == 7
ints.get(7) - > IndexOutOfBoundsException






显然,第二个例子中的代码是不是你实际上有什么。我怀疑你实际上的代码是:

  List< Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList()); 
System.out.print(list:); $(b)$ b $ ints.forEach((i) - > {
System.out.print(i +);
^^^^^^^< - 这是不同的
});

与您发布的内容完全不同。


Why when I use following code I get IndexOutOfBoundsException

Code:

    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) + " ");
    });

My error stack:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 5
    at java.util.ArrayList.rangeCheck(ArrayList.java:638)
    at java.util.ArrayList.get(ArrayList.java:414)
    at Agent.lambda$main$1(Agent.java:33)
    at Agent$$Lambda$8/980546781.accept(Unknown Source)
    at java.util.ArrayList.forEach(ArrayList.java:1234)
    at Agent.main(Agent.java:32)
the list: Java Result: 1

but When I change my list to one digit numbers everything is fine

Code:

    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) + " ");
    });

output:

2 8 7 4 3 

解决方案

I think the reason is pretty clear.

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});

Translates approximately to:

for (Integer i : ints) {
    System.out.println(ints.get(i - 1) + " ");
}

Which will cause IndexOutOfBoundsExceptions because i refers to the elements of each list, and each of those elements - 1 will give an index that is clearly out of bounds. For your first example, i will be 21, which gives an index of 21 - 1 == 20, which is out of bounds for the list you created.

Example:

List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());

will end up so that

ints == [21, 22, 32, 42, 52]

So when you run this:

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});

The computer takes the first element and tries to execute the body of the lambda:

Execute System.out.print(ints.get(i-1) + " ");:
    First element is 21
    21 - 1 == 20
    ints.get(20) --> IndexOutOfBoundsException

And for your second example:

List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());

becomes

ints == [2, 8, 7, 4, 3]

So when you run this:

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});

The computer goes through the elements and tries to execute the body of the lambda:

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


So evidently the code in your second example is not what you actually have. I suspect that the code you actually have is:

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
});

Which is entirely different than what you posted.

这篇关于在JAVA中使用Lambda表达式进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆