for 循环遍历数组
- 数组遍历
- 把数组元素依次取出来
- 遍历取值
int[] intArray = new int[]{11,22,33,44,55,66,77,88,99}; for(int i = 0; i <= intArray.Length - 1; i++) //intArray.Length:取得当前数组长度. { Console.WriteLine(intArray[i]); } Console.ReadKey();
- 遍历赋值
string[] str = new string[5]; for (int i = 0; i <= str.Length - 1; i++) { str[i] = "元素" + i; } for (int j = 0; j <= str.Length; j++) { Console.WriteLine(str[j]); } Console.ReadKey();
- 练习:定义一个字符串数组存放12生肖,循环遍历该数组,将元素组合成一个字符串,且元素与元素之间用' | '分割。12生肖数组元素如下:"子鼠","丑牛","寅虎