上面的语句将获取数组中第 3 行第 4 个元素。您可以通过上面的示意图来进行验证。让我们来看看下面的程序,我们将使用嵌套循环来处理二维数组:
using System;namespace Arrayapplication{ class MyArray { static void Main(string[] args) { /* 一个带有 5 行 2 列的数组 */ int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} }; int i, j; /* 输出数组中每个元素的值 */ for (i = 0; i < 5; i++) { for (j = 0; j < 2; j++) { Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]); } } Console.ReadKey(); } }}当上面的代码被编译和执行时,它会产生下列结果:a[0,0]: 0a[0,1]: 0a[1,0]: 1a[1,1]: 2a[2,0]: 2a[2,1]: 4a[3,0]: 3a[3,1]: 6a[4,0]: 4a[4,1]: 8========C#基础整理(多维数组)
http://www.cnblogs.com/wleaves/p/4172558.html多维数组1、二维数组:表示方法:int[y,x],x、y是索引,y代表行,x代表列。例:int[,] second = new int[2, 3]{{3,2,5},{6,7,8}};//{}可以不写修改方法:second[0, 1] = 3;//表示将第0行第1列的数字改为3练习:用二维数组进行冒泡排序:输入人数,输入每个人的年龄、身高、姓名,求平均年龄,按身高从高到低排序Console.WriteLine("请输入人数:"); int n = int.Parse(Console.ReadLine()); string[,] ren = new string[n, 3]; //分别录入每个学生的信息 for (int i = 0; i < n; i++) { Console.WriteLine("请输入姓名、年龄、身高,用回车键分隔:"); for(int j = 0 ;j<3;j++) { ren[i, j] = Console.ReadLine(); } } double sum = 0; //计算总年龄,打印平均年龄 for(int i = 0;i<n;i++) { sum = sum +int.Parse(ren[i,1]); } Console.WriteLine("平均年龄为:{0}",Math.Floor(sum/n)); Console.WriteLine("姓名 年龄 身高"); //根据身高进行排序 for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (int.Parse(ren[j, 2]) > int.Parse(ren[i, 2])) { string[] zhong = {ren[j,0],ren[j,1],ren[j,2]}; //交换所有信息,使身高的排序与姓名、年龄保持一致 ren[j, 0] = ren[i, 0]; ren[j, 1] = ren[i, 1]; ren[j, 2] = ren[i, 2]; ren[i, 0] = zhong[0]; ren[i, 1] = zhong[1]; ren[i, 2] = zhong[2]; } } } int [,] ab = new int[0,0]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { Console.Write(ren[i, j]+" "); } Console.Write("/n"); }*2、多维数组写法:int[z,y,x]:z表示有几个二维数组,使用方法同二维数组========c#多维数组的建立及操作总结
http://blog.csdn.net/u011555996/article/details/534654681C#如何定义和使用多维数组不建议使用ArrayList,当数组里的元素是值类型在操作的时候会出现大量的装箱与拆箱步骤性能会损失许多,而是应该用什么类型的元素创建什么类型的数组,除非你的元素有交叉或不确定才考虑采用ArrayList。多维数组定义如下:数组可以具有多个维度。例如,下列声明创建一个四行两列的二维数组:C#int[,]array = new int[4, 2];另外,下列声明创建一个三维(4、2 和 3)数组:C#int[,,] array1 = new int[4, 2, 3];数组初始化可以在声明数组时将其初始化,如下例所示:C#int[,]array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };int[, ,] array3D = new int[,,] { { { 1, 2, 3 } }, { { 4, 5, 6 } } };也可以初始化数组但不指定级别:C#int[,]array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };如果选择声明一个数组变量但不将其初始化,必须使用 new 运算符将一个数组分配给此变量。例如:C#int[,]array5;array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK//array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error也可以给数组元素赋值,例如:C#array5[2,1] = 25;写一个详细的小例子,用一个动态的STRING型数组存放一个TABLE中的每一个单元中的内容: DataTabledt = ds.Tables[0];//取出一个内容是动态的表 string[,] str = newstring[dt.Rows.Count,dt.Columns.Count]; //用数组str来存放一个TABLE中的每一个单元中的内容 //dt.Rows.Count是表的行数,dt.Columns.Count是表的例数 for(int i =0;i < dt.Rows.Count ; i++) { for(int j=0;j<dt.Columns.Count;j++) { str[i,j] =dt.Rows[i][j].ToString().Trim(); } }2 定义不定长数组一维:int[] numbers = new int[]{1,2,3,4,5,6}; //不定长int[] numbers = new int[3]{1,2,3};//定长多维int[,] numbers = newint[,]{{1,2,3},{1,2,3}}; //不定长int[,] numbers = new int[2,2]{{1,2},{1,2}};//定长实例:A:int[] mf1=newint[6]; //注意初始化数组的范围,或者指定初值; //包含6个元素的一维整数数组,初值1,2,3,4,5,6 int[]mf2=new int[6]{1,2,3,4,5,6};B://一维字符串数组,如果提供了初始值设定项,则还可以省略 new 运算符 string[] mf3={"c","c++","c#"};C://一维对象数组 Object[] mf4 = new Object[5] { 26, 27, 28, 29, 30 };D://二维整数数组,初值mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4 int[,]mf5=new int[,]{{1,2},{3,4}};E://6*6的二维整型数组 int[,]mf6=new mf[6,6];3 实例定义一维和二维数组using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class PRogram { static void Main(string[] args) { //一维数组定义与初始化 int[] one1 = new int[] {3,2,1 };//第一种方式 int[] one2 = { 3, 2, 1 }; //第二种方式 int[] one3; //第三种方式 one3=new int[]{3,2,1}; //二维数组定义与初始化 //不规则二维数组 int[][] array = new int[2][]; array[0] = new int[3]; array[0][1] = 11; array[0][2] = 12; array[1] = new int[] { 1, 2, 3, 4,5 }; //或int[][] array = new int[2][] { new int[] {0,11,12 }, new int[] {1,2,3,4,5 }}; Console.WriteLine( "不规则二维数组: "); for (int i = 0; i < array.Length; i++) { foreach (int j in array[i]) { Console.Write(j+" "); } Console.WriteLine(); } //固定长度的矩阵数组 int[,] arrray1=new int[2,5]{{9,9,9,9,0},{0,0,9,0,0}}; Console.WriteLine("规则二维数组输出方法一: "); for (int ii = 0; ii < 2; ii++) //输出方法一 { for (int j = 0; j < 5; j++) { Console.Write(arrray1[ii,j] + " "); } Console.WriteLine(); } Console.WriteLine("规则二维数组输出方法二: "); foreach (int j in arrray1)//arrray.length=10; //输出方法二 { Console.Write(j + " "); } Console.WriteLine(); Console.ReadLine(); } } } 4 对数组操作取得数组元素个数: int b; b = sizeof (a)/sizeof (*a); c#字符串及数组操作2007-12-12 17:53字符串操作(取当前时间)stringtime=convert.tostring(DateTime.Today).split( new char []{''}); textbox1.text=time[0]; 以空格作为分界点; 1)数组概述C# 数组从零开始建立索引,即数组索引从零开始。C#中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。int[] table; // not int table[];另一细节是,数组的大小不是其类型的一部分,而在 C语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int对象的任意数组,而不管数组长度如何。int[] numbers; // declare numbers as an intarray of any sizenumbers = new int[10]; // numbers is a10-element arraynumbers = new int[20]; // now it's a 20-elementarray2)声明数组C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组:一维数组:int[] numbers;多维数组:string[,] names;数组的数组(交错的):byte[][] scores; 声明数组(如上所示)并不实际创建它们。在C#中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:一维数组:int[] numbers = new int[5];多维数组:string[,] names = new string[5,4];数组的数组(交错的):byte[][] scores = new byte[5][]; for (int x = 0; x <scores.Length; x++) {scores[x] = new byt[4];}还可以有更大的数组。例如,可以有三维的矩形数组:int[,,] buttons = new int[4,5,3];甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组int[][,,][,] numbers;3)初始化数组C# 通过将初始值括在大括号 ({}) 内为在声明时初始化数组提供了简单而直接了当的方法。下面的示例展示初始化不同类型的数组的各种方法。注意如果在声明时没有初始化数组,则数组成员将自动初始化为该数组类型的默认初始值。另外,如果将数组声明为某类型的字段,则当实例化该类型时它将被设置为默认值 null。一维数组int[] numbers = new int[5] {1, 2, 3, 4, 5};string[] names = new string[3]{"Matt", "Joanne", "Robert"}; 可省略数组的大小,如下所示:int[] numbers = new int[] {1, 2, 3, 4, 5};string[] names = new string[]{"Matt", "Joanne", "Robert"}; 如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:int[] numbers = {1, 2, 3, 4, 5};string[] names = {"Matt","Joanne", "Robert"}; 多维数组int[,] numbers = new int[3, 2] { {1, 2},{3, 4}, {5, 6} };string[,] siblings = new string[2, 2] {{"Mike","Amy"}, {"Mary","Albert"} };可省略数组的大小,如下所示:int[,] numbers = new int[,] { {1, 2}, {3,4}, {5, 6} };string[,] siblings = new string[,] {{"Mike","Amy"}, {"Mary","Albert"} }; 如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:int[,] numbers = { {1, 2}, {3, 4}, {5, 6}};string[,] siblings = { {"Mike","Amy"}, {"Mary", "Albert"} };交错的数组(数组的数组)可以像下例所示那样初始化交错的数组:int[][] numbers = new int[2][] { new int[]{2,3,4}, new int[] {5,6,7,8,9} };可省略第一个数组的大小,如下所示:int[][] numbers = new int[][] { new int[]{2,3,4}, new int[] {5,6,7,8,9} };-或-int[][] numbers = { new int[] {2,3,4}, newint[] {5,6,7,8,9} };请注意,对于交错数组的元素没有初始化语法。 4)访问数组成员访问数组成员可以直接进行,类似于在 C/C++ 中访问数组成员。例如,下面的代码创建一个名为 numbers 的数组,然后向该数组的第五个元素赋以 5:int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3,2, 1, 0};numbers[4] = 5; 下面的代码声明一个多维数组,并向位于 [1, 1] 的成员赋以 5:int[,] numbers = { {1, 2}, {3, 4}, {5, 6},{7, 8}, {9, 10} };numbers[1, 1] = 5; 下面声明一个一维交错数组,它包含两个元素。第一个元素是两个整数的数组,第二个元素是三个整数的数组:int[][] numbers = new int[][] { new int[]{1, 2}, new int[] {3, 4, 5}}; 下面的语句向第一个数组的第一个元素赋以 58,向第二个数组的第二个元素赋以 667:numbers[0][0] = 58;numbers[1][1] = 667; 5)数组是对象在 C# 中,数组实际上是对象。System.Array 是所有数组类型的抽象基类型。可以使用 System.Array 具有的属性以及其他类成员。这种用法的一个示例是使用“长度”(Length) 属性获取数组的长度。下面的代码将 numbers 数组的长度(为 5)赋给名为 LengthOfNumbers 的变量:int[] numbers = {1, 2, 3, 4, 5};int LengthOfNumbers = numbers.Length;System.Array 类提供许多有用的其他方法/属性,如用于排序、搜索和复制数组的方法。 6)对数组使用 foreachC# 还提供 foreach 语句。该语句提供一种简单、明了的方法来循环访问数组的元素。例如,下面的代码创建一个名为 numbers 的数组,并用 foreach 语句循环访问该数组:int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1,0};foreach (int i innumbers){System.Console.WriteLine(i);}由于有了多维数组,可以使用相同方法来循环访问元素,例如:int[,] numbers = new int[3, 2] {{9, 99},{3, 33}, {5, 55}};foreach(int i innumbers){Console.Write("{0} ", i);}该示例的输出为: 9 99 3 33 5 55不过,由于有了多维数组,使用嵌套 for 循环将使您可以更好地控制数组元素========C# 二维数组
http://www.cnblogs.com/mdnx/archive/2012/09/02/2668032.html
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 定义一个二维数组. 其实可以把二维数组看作一个表 例如 /* 0 1 2 3 4 ------------列等于i 维 ---------------- * 0 | | | | | | ---------------- * 1 | | | | | | * ---------------- * 2 | | | | | | * ---------------- * 3 | | | | | | * ↓ ---------------- ↓ * ↓ 行代表 j 维 */ int[,] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // 这是一个三行三列的二维数组. // 现在我们来把这个二维数组输出在屏幕上. 用两个for循环来实现, 一个控制行,一个控制列. for (int i = 0; i < 3; i++) // 因为只有三行, 而且数组下标是从0开始的,所以要小于三 { for (int j = 0; j < 3; j++) //同理, 只有三列, 所以要小于三. { Console.Write(array[i,j] + "/t"); } Console.WriteLine(); } /* 打印结果为 1 2 3 * 4 5 6 * 7 8 9 */ // 接下来做一个实例 , 用二维数组打印一个矩阵出来.. string[,] Chess = new string[11, 19]; //声明一个二维数组. for (int a = 0; a < 11; a++) // 控制行, { for (int b = 0; b < 19; b++) //控制列 { if (b % 2 != 0) Chess[a, b] = "—"; else Chess[a, b] = "|"; Console.Write(Chess[a, b]); } Console.WriteLine(); } /* 打印结果 |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| |—|—|—|—|—|—|—|—|—| */ } } }========多维数组(C# 编程指南)
https://msdn.microsoft.com/zh-cn/library/2yd9wwz4 若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 2017 RC 文档。数组可以具有多个维度。 例如,下列声明创建一个四行两列的二维数组。C# int[,] array = new int[4, 2];下列声明创建一个三维(4、2 和 3)数组。C# int[, ,] array1 = new int[4, 2, 3];数组初始化可以在声明数组时将其初始化,如下例所示。C# // Two-dimensional array. int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // The same array with dimensions specified. int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // A similar array with string elements. string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" }, { "five", "six" } }; // Three-dimensional array. int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // The same array with dimensions specified. int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // accessing array elements. System.Console.WriteLine(array2D[0, 0]); System.Console.WriteLine(array2D[0, 1]); System.Console.WriteLine(array2D[1, 0]); System.Console.WriteLine(array2D[1, 1]); System.Console.WriteLine(array2D[3, 0]); System.Console.WriteLine(array2Db[1, 0]); System.Console.WriteLine(array3Da[1, 0, 1]); System.Console.WriteLine(array3D[1, 1, 2]); // Getting the total count of elements or the length of a given dimension. var allLength = array3D.Length; var total = 1; for (int i = 0; i < array3D.Rank; i++) { total *= array3D.GetLength(i); } System.Console.WriteLine("{0} equals {1}", allLength, total); // Output: // 1 // 2 // 3 // 4 // 7 // three // 8 // 12 // 12 equals 12也可以初始化数组但不指定级别。C# int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };如果选择声明一个数组变量但不将其初始化,必须使用 new 运算符将一个数组分配给此变量。 以下示例显示 new 的用法。C# int[,] array5; array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK //array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error以下示例将值分配给特定的数组元素。C# array5[2, 1] = 25;同样,下面的示例获取特定数组元素的值,并将它赋给变量elementValue。C# int elementValue = array5[2, 1];以下代码示例将数组元素初始化为默认值(交错数组除外):C# int[,] array6 = new int[10, 10];========交错数组(C# 编程指南)
https://msdn.microsoft.com/zh-cn/library/2s05feca.aspx 若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 2017 RC 文档。交错数组是元素为数组的数组。 交错数组元素的维度和大小可以不同。 交错数组有时称为“数组的数组”。以下示例说明如何声明、初始化和访问交错数组。下面声明一个由三个元素组成的一维数组,其中每个元素都是一个一维整数数组:C# int[][] jaggedArray = new int[3][];必须初始化 jaggedArray 的元素后才可以使用它。 可以如下例所示初始化该元素:C# jaggedArray[0] = new int[5]; jaggedArray[1] = new int[4]; jaggedArray[2] = new int[2];每个元素都是一个一维整数数组。 第一个元素是由 5 个整数组成的数组,第二个是由 4 个整数组成的数组,而第三个是由 2 个整数组成的数组。也可以使用初始值设定项用值填充数组元素,在这种情况下不需要数组大小。 例如:C# jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 }; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 };还可以在声明数组时将其初始化,如:C# int[][] jaggedArray2 = new int[][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} };可以使用下面的速记格式。 请注意:不能从元素初始化中省略 new 运算符,因为不存在元素的默认初始化:C# int[][] jaggedArray3 = { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} };交错数组是数组的数组,因此其元素是引用类型并初始化为 null。可以如下例所示访问个别数组元素:C# // Assign 77 to the second element ([1]) of the first array ([0]): jaggedArray3[0][1] = 77; // Assign 88 to the second element ([1]) of the third array ([2]): jaggedArray3[2][1] = 88;可以混合使用交错数组和多维数组。 下面声明和初始化一个一维交错数组,该数组包含大小不同的三个二维数组元素。 有关二维数组的详细信息,请参阅多维数组。C# int[][,] jaggedArray4 = new int[3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22}, {99,88}, {0,9} } };可以如本例所示访问个别元素,该示例显示第一个数组的元素 [1,0] 的值(值为 5):C# System.Console.Write("{0}", jaggedArray4[0][1, 0]);方法 Length 返回包含在交错数组中的数组的数目。 例如,假定您已声明了前一个数组,则此行:C# System.Console.WriteLine(jaggedArray4.Length);返回值 3。示例本例生成一个数组,该数组的元素为数组自身。 每一个数组元素都有不同的大小。C#class ArrayTest{ static void Main() { // Declare the array of two elements: int[][] arr = new int[2][]; // Initialize the elements: arr[0] = new int[5] { 1, 3, 5, 7, 9 }; arr[1] = new int[4] { 2, 4, 6, 8 }; // Display the array elements: for (int i = 0; i < arr.Length; i++) { System.Console.Write("Element({0}): ", i); for (int j = 0; j < arr[i].Length; j++) { System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " "); } System.Console.WriteLine(); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); }}/* Output: Element(0): 1 3 5 7 9 Element(1): 2 4 6 8*/========
新闻热点
疑难解答