From 5d9da9aa550f5a03fabd121340255231d0afe99f Mon Sep 17 00:00:00 2001 From: youyihj Date: Sun, 16 Oct 2022 22:39:58 +0800 Subject: [PATCH] indexed foreach --- advanced/loops.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/advanced/loops.md b/advanced/loops.md index dd8a956..8a527be 100644 --- a/advanced/loops.md +++ b/advanced/loops.md @@ -71,13 +71,24 @@ while(j >= 0) { ```csharp // 定义一个数组并遍历 -var numbers as int[] = [2,4,8,16,32,64]; +var numbers as int[] = [2, 4, 8]; for number in numbers { print(number); } -// 输出 2, 4, 8, 16, 32, 64 +// 输出 2, 4, 8 + +// 对于数组的 foreach,可以使用带索引的遍历法 +for i, number in numbers { + // i 变量代表现在遍历到哪个索引下的元素 + print("Index: " + i + ", Element: " + number); +} + +// 依次输出 +// Index: 0, Element: 2 +// Index: 1, Element: 4 +// Index: 2, Element: 8 -// 定义一个 Map 映射并遍历 +// 定义一个 Map (映射)并遍历 var map as string[int] = { 1 : "一", 2 : "二",