本文主要是介绍2022年5月15日【Jiawei_Z】C# 基础教程--遍历数组数组内存结构,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C#的基础教程
https://www.bilibili.com/video/BV1c4411s7UV?p=13
数组的遍历
数组长度获取: 数组名.length;
演示 1: for 语句循环赋值
演示 2: for 语句循环取值
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; 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();
遍历数组的方法
//foreach 遍历数组. int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach(int i in intArray){Console.WriteLine(i);}Console.ReadKey();
运行快捷键 F5
进入看代码块快捷键F12
int[] intArray = new int[5]; //元素默认值:0float[] fArray = new float[5]; //元素默认值:0double[] dArray = new double[5]; //元素默认值:0string[] sArray = new string[5]; //元素默认值:NULLbool[] bArray = new bool[5]; //元素默认值:Flase//遍历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 dArray){Console.WriteLine(d);}Console.WriteLine("--------------------------");//遍历string[]foreach (string s in sArray){Console.WriteLine(s);}Console.WriteLine("--------------------------");//遍历bool[]foreach (bool b in bArray){Console.WriteLine(b);}Console.ReadKey();
数值传递 和 引用传递
https://www.bilibili.com/video/BV1c4411s7UV?p=14
数值类型和引用类型
//1.演示:int 类型变量间传值.int a = 10;int b = a; //变量间的传值. b = 10; 值传递.b = 20;Console.WriteLine("a的值是{0},b的值是{1}", a, b); //a:10 b:20 -------输出在一行中Console.WriteLine("--------------------------");Console.WriteLine("a的值是{0}\r\nb的值是{1}", a, b); //a:10 b:20 -------分2行输出 加上 \r\nConsole.ReadKey();
//演示:int 数组类型变量间传值.int[] intA = new int[] { 111, 222, 333, 444 };int[] intB = intA; //变量间的传值. 引用.intB[0] = 100;intB[3] = 999;foreach(int i in intA){Console.WriteLine(i);}Console.WriteLine("--------------------------");foreach(int j in intB){Console.WriteLine(j);}Console.ReadKey();
这篇关于2022年5月15日【Jiawei_Z】C# 基础教程--遍历数组数组内存结构的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!