我的代码:
List<Integer> ints = Stream.of(1,2,4,3,5).collect(Collectors.toList());
ints.forEach((i)-> System.out.print(ints.get(i-1)+ " "));
Run Code Online (Sandbox Code Playgroud)
出局:
1 2 3 4 5
我的问题是为什么我必须在get方法中使用i-1?i-1会阻止边界问题吗?
下面的代码是否像for循环迭代一样?
(i)-> System.out.print(ints.get(i-1))
Run Code Online (Sandbox Code Playgroud)
所以上面的代码等于这个
for(Ineger i:ints)
System.out.print(ints.get(i));
Run Code Online (Sandbox Code Playgroud)