using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
namespace ForEachStruct
{
internal class Program
{
static void Main(string[] args)
{
//1.for循环遍历数组
/*
int[] intArray;
intArray = new int[5];
intArray[0] = 10;
intArray[1] = 12;
intArray[2] = 14;
Console.WriteLine(intArray[1]);
Console.ReadKey();
*/
/*
int[] intArray = new int[] { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
//遍历数组.
//intArray.length:可以取得当前数组的长度.9.
//Console.WriteLine(intArray.Length);
for(int i =0;i <= intArray.Length - 1; i++)
{
Console.WriteLine(intArray[i]);
}
Console.ReadKey();
*/
/*
string[] str = new string[5]; //数组声明与初始化
//数组的元素赋值.
for(int i =0; i < str.Length; i++)
{
str[i] = "元素" + i;
}
//循环取值.
for(int j = 0; j < str.Length; j++)
{
Console.WriteLine(str[j]);
}
Console.ReadKey();
*/
//例:定义一个字符串数组,存放12生肖,循环遍历这个数组,将元素
//组合成一个字符串,且元素与元素之间用'|'分割。
//12生肖数组元素如下:"子鼠","丑牛","寅虎","巳蛇","午马","未羊","
//"申猴","酉鸡","戌狗","亥猪"
/*
string[] animals = new string[] {"子鼠","丑牛","寅虎","巳蛇","午马","未羊","申猴","酉鸡","戌狗","亥猪"};
String animalStr = "";
for (int i = 0; i < animals.Length; i++)
{
animalStr += animals[i];
if (i <animals.Length - 1)
{
animalStr += "|";
}
}
Console.WriteLine(animalStr);
Console.ReadKey();
*/
//2.foreach遍历数组
//语法
//foreach(数组数据类型 临时变量 in 数组名)
//{
// Console.WriteLine(临时变量)
//}
/*
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9};
foreach (int i in intArray)
{
Console.WriteLine(i);
}
Console.ReadKey();
*/
//3.数组元素的初始值
//数据类型[] 数组名 = new 数据类型[数组长度];
//数组的声明,初始化.五个元素的存储空间,已经在内存中开辟了.
int[] intArray = new int[5];
float[] fArray = new float[5];
double[] dArrary = new double[5];
string[] sArrary = new string[5];
bool[] bArrary = new bool[5];
//数组初始化完成后,各种类型的数组的默认值
//int[], 元素的值默认都是0;
//float[],元素的值默认都是0;
//double[], 元素的值默认都是0.0;
//string[],元素的值默认都是null;
//bool[], 元素的值默认都是false;
//遍历 int[]
foreach (int i in intArray)
{
Console.WriteLine(i);
}
Console.WriteLine("------------");
//遍历 float[]
foreach (int f in fArray)
{
Console.WriteLine(f) ;
}
Console.WriteLine("------------");
//遍历 double[]
foreach (int d in dArrary)
{
Console.WriteLine(d);
}
Console.WriteLine("------------");
//遍历 string[]
foreach (string s in sArrary)
{
Console.WriteLine(s);
}
Console.WriteLine("------------");
//遍历 bool[]
foreach (bool b in bArrary)
{
Console.WriteLine(b);
}
Console.ReadKey();
}
}
}