1. Swift中for in循环和Java中foreach很类似,都可以简化对集合的遍历操作。
Swift语法:
for index in 1...5{ print("the index is \(index)") } let base = 3 let power = 3 var answer = 1 for _ in 1...power { answer *= base } print("\(base) to the power of \(power) is \(answer)") // 输出 "3 to the power of 10 is 59049"
Java语法如下:
ArrayList<String> array = new ArrayList<String>(); array.add("a"); array.add("b"); array.add("c"); array.add("d"); for(String item: array){ System.out.println(item); }
时间: 2024-10-21 11:18:25