数组c#小叙一维二维数组.doc





《数组c#小叙一维二维数组.doc》由会员分享,可在线阅读,更多相关《数组c#小叙一维二维数组.doc(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流数组 c#小叙一维二维数组.精品文档.关于C#中的一维数组、二维数组、交错数组的概念、定义及应用数组概述C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。声明数组时,方括号 () 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。int table; / not int table; 另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int 对象的任意数组,
2、而不管数组长度如何。int numbers; / declare numbers as an int array of any sizenumbers = new int10; / numbers is a 10-element arraynumbers = new int20; / now its a 20-element array声明数组C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组:一维数组:int numbers;多维数组:string, names;数组的数组(交错的):byte scores;声明数组(如上所示)并不实际创
3、建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:一维数组:int numbers = new int5;多维数组:string, names = new string5,4;数组的数组(交错的):byte scores = new byte5;for (int x = 0; x scores.Length; x+) scoresx = new byte4;还可以有更大的数组。例如,可以有三维的矩形数组:int, buttons = new int4,5,3;甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三
4、维数组的一维数组。int, numbers;示例下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。/ arrays.csusing System;class DeclareArraysSample public static void Main() / Single-dimensional array int numbers = new int5; / Multidimensional array string, names = new string5,4; / Array-of-arrays (jagged array) byte scores = new byte5; / Cr
5、eate the jagged array for (int i = 0; i scores.Length; i+) scoresi = new bytei+3; / Print length of each row for (int i = 0; i scores.Length; i+) Console.WriteLine(Length of row 0 is 1, i, scoresi.Length);输出Length of row 0 is 3Length of row 1 is 4Length of row 2 is 5Length of row 3 is 6Length of row
6、 4 is 7初始化数组C# 通过将初始值括在大括号 () 内为在声明时初始化数组提供了简单而直接了当的方法。特别要注意的是,如果声明时未初始化数组,则数组成员自动初始化为该数组类型的默认初始值。 下面的示例展示初始化不同类型的数组的各种方法。一维数组int numbers = new int5 1, 2, 3, 4, 5;string names = new string3 Matt, Joanne, Robert;可省略数组的大小,如下所示:int numbers = new int 1, 2, 3, 4, 5;string names = new string Matt, Joanne,
7、 Robert;如果提供了初始值设定项,还可省略 new 语句,如下所示:int numbers = 1, 2, 3, 4, 5;string names = Matt, Joanne, Robert;多维数组int, numbers = new int3, 2 1, 2, 3, 4, 5, 6 ;string, siblings = new string2, 2 Mike,Amy, Mary,Albert ;可省略数组的大小,如下所示:int, numbers = new int, 1, 2, 3, 4, 5, 6 ;string, siblings = new string, Mike,A
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数组 c#小叙一维二维数组 c# 小叙一维 二维

限制150内