给出以下用Java编写的代码片段,有没有一种方法可以优雅地用Python实现相同的结果
班级:
public class SomeClass {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
goOverList(list.size(), (index) -> list.add(String.valueOf(index + 1)));
list.forEach(System.out::println);
}
public static void goOverList(int listSize, Fun fun) {
for (int i = 0; i < listSize; i++) {
fun.doStuff(i);
}
}
}
接口:
public interface Fun {
void doStuff(int index);
}
输出:
One
Two
Three
1
2
3
其思想是将任何函数作为参数传递,并获取要使用的索引
5行Python:
输出:
我能赢什么
相关问题 更多 >
编程相关推荐