【发布时间】:2021-04-26 23:26:09
【问题描述】:
下面是嵌套 for 循环的命令式风格。我想利用 java 8 Streams 和其他特性使其成为一种功能样式。
import java.util.Arrays;
public class Java8 {
public static final String TAILS = "TAILS";
public static final String HEADS = "HEADS";
public static void main(String[] args) {
String[] coines = new String[11];
Arrays.fill(coines, TAILS);
for(int i=1;i<coines.length ; i++){
System.out.println("Person :" + i);
for (int j=1; j < coines.length ; j++){
if(j%i==0){
System.out.print("Flipping " + j +"th Element from " + coines[j] + " to ");
coines[j] = coines[j]== TAILS ? HEADS : TAILS;
System.out.println(coines[j]);
}
}
}
}
}
【问题讨论】:
-
这能回答你的问题吗? Converting Nested For Loops To Streams
标签: arrays java-8 functional-programming java-stream