进阶:多维数组

了解了二维数组,我们来了解下多维数组

现在你可以声明一个 string 变量的二维数组,如下:
string [,] names;
或者,你可以声明一个 int 变量的三维数组,如下:
int [ , , ] m;
二维数组
多维数组最简单的形式是二维数组。一个二维数组,在本质上,是一个一维数组的列表。
一个二维数组可以被认为是一个带有 x 行和 y 列的表格。下面是一个二维数组,包含 3 行和 4 列:
C# 中的二维数组
因此,数组中的每个元素是使用形式为 a[ i , j ] 的元素名称来标识的,其中 a 是数组名称,i 和 j 是唯一标识 a 中每个元素的下标。
初始化二维数组
多维数组可以通过在括号内为每行指定值来进行初始化。下面是一个带有 3 行 4 列的数组。
int [,] a = new int [3,4] {
 {0, 1, 2, 3} ,   /*  初始化索引号为 0 的行 */
 {4, 5, 6, 7} ,   /*  初始化索引号为 1 的行 */
 {8, 9, 10, 11}   /*  初始化索引号为 2 的行 */
};
访问二维数组元素
二维数组中的元素是通过使用下标(即数组的行索引和列索引)来访问的。例如:
int val = a[2,3];

上面的语句将获取数组中第 3 行第 4 个元素。您可以通过上面的示意图来进行验证。让我们来看看下面的程序,我们将使用嵌套循环来处理二维数组:

 
  1. using System;

  2. namespace ArrayApplication

  3. {

  4. class MyArray

  5. {

  6. static void Main(string[] args)

  7. {

  8. /* 一个带有 5 行 2 列的数组 */

  9. int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };

  10. int i, j;

  11. /* 输出数组中每个元素的值 */

  12. for (i = 0; i < 5; i++)

  13. {

  14. for (j = 0; j < 2; j++)

  15. {

  16. Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);

  17. }

  18. }

  19. Console.ReadKey();

  20. }

  21. }

  22. }


当上面的代码被编译和执行时,它会产生下列结果:

a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[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
练习:用二维数组进行冒泡排序:

输入人数,输入每个人的年龄、身高、姓名,求平均年龄,按身高从高到低排序
 

 
  1. Console.WriteLine("请输入人数:");

  2. int n = int.Parse(Console.ReadLine());

  3. string[,] ren = new string[n, 3];

  4. //分别录入每个学生的信息

  5. for (int i = 0; i < n; i++)

  6. {

  7. Console.WriteLine("请输入姓名、年龄、身高,用回车键分隔:");

  8. for(int j = 0 ;j<3;j++)

  9. {

  10. ren[i, j] = Console.ReadLine();

  11. }

  12. }

  13. double sum = 0;

  14. //计算总年龄,打印平均年龄

  15. for(int i = 0;i<n;i++)

  16. {

  17. sum = sum +int.Parse(ren[i,1]);

  18. }

  19. Console.WriteLine("平均年龄为:{0}",Math.Floor(sum/n));

  20. Console.WriteLine("姓名 年龄 身高");

  21. //根据身高进行排序

  22. for (int i = 0; i < n; i++)

  23. {

  24. for (int j = i; j < n; j++)

  25. {

  26. if (int.Parse(ren[j, 2]) > int.Parse(ren[i, 2]))

  27. {

  28. string[] zhong = {ren[j,0],ren[j,1],ren[j,2]};

  29. //交换所有信息,使身高的排序与姓名、年龄保持一致

  30. ren[j, 0] = ren[i, 0];

  31. ren[j, 1] = ren[i, 1];

  32. ren[j, 2] = ren[i, 2];

  33. ren[i, 0] = zhong[0];

  34. ren[i, 1] = zhong[1];

  35. ren[i, 2] = zhong[2];

  36. }

  37. }

  38. }

  39. int [,] ab = new int[0,0];

  40. for (int i = 0; i < n; i++)

  41. {

  42. for (int j = 0; j < 3; j++)

  43. {

  44. Console.Write(ren[i, j]+" ");

  45. }

  46. Console.Write("\n");

  47. }



*2、多维数组

写法:int[z,y,x]:z表示有几个二维数组,使用方法同二维数组
========

c#多维数组的建立及操作总结

http://blog.csdn.net/u011555996/article/details/53465468
1C#如何定义和使用多维数组
不建议使用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 实例定义一维和二维数组
 

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. namespace ConsoleApplication1

  6. {

  7. class Program

  8. {

  9. static void Main(string[] args)

  10. {

  11. //一维数组定义与初始化

  12. int[] one1 = new int[] {3,2,1 };//第一种方式

  13. int[] one2 = { 3, 2, 1 }; //第二种方式

  14. int[] one3; //第三种方式

  15. one3=new int[]{3,2,1};

  16. //二维数组定义与初始化

  17. //不规则二维数组

  18. int[][] array = new int[2][];

  19. array[0] = new int[3];

  20. array[0][1] = 11;

  21. array[0][2] = 12;

  22. array[1] = new int[] { 1, 2, 3, 4,5 };

  23. //或int[][] array = new int[2][] { new int[] {0,11,12 }, new int[] {1,2,3,4,5 }};

  24. Console.WriteLine( "不规则二维数组: ");

  25. for (int i = 0; i < array.Length; i++)

  26. {

  27. foreach (int j in array[i])

  28. {

  29. Console.Write(j+" ");

  30. }

  31. Console.WriteLine();

  32. }

  33. //固定长度的矩阵数组

  34. int[,] arrray1=new int[2,5]{{9,9,9,9,0},{0,0,9,0,0}};

  35. Console.WriteLine("规则二维数组输出方法一: ");

  36. for (int ii = 0; ii < 2; ii++) //输出方法一

  37. {

  38. for (int j = 0; j < 5; j++)

  39. {

  40. Console.Write(arrray1[ii,j] + " ");

  41. }

  42. Console.WriteLine();

  43. }

  44. Console.WriteLine("规则二维数组输出方法二: ");

  45. foreach (int j in arrray1)//arrray.length=10; //输出方法二

  46. {

  47. Console.Write(j + " ");

  48. }

  49. Console.WriteLine();

  50. Console.ReadLine();

  51. }

  52. }

  53. }



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 size
numbers = new int[10]; // numbers is a10-element array
numbers = new int[20]; // now it's a 20-elementarray
2)声明数组
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)对数组使用 foreach
C# 还提供 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

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. namespace ConsoleApplication1

  6. {

  7. class Program

  8. {

  9. static void Main(string[] args)

  10. {

  11. // 定义一个二维数组. 其实可以把二维数组看作一个表 例如

  12. /* 0 1 2 3 4 ------------列等于i 维

  13. ----------------

  14. * 0 | | | | | |

  15. ----------------

  16. * 1 | | | | | |

  17. * ----------------

  18. * 2 | | | | | |

  19. * ----------------

  20. * 3 | | | | | |

  21. * ↓ ----------------

  22. * ↓

  23. 行代表 j 维

  24. */

  25. int[,] array = { { 1, 2, 3 },

  26. { 4, 5, 6 },

  27. { 7, 8, 9 } }; // 这是一个三行三列的二维数组.

  28. // 现在我们来把这个二维数组输出在屏幕上. 用两个for循环来实现, 一个控制行,一个控制列.

  29. for (int i = 0; i < 3; i++) // 因为只有三行, 而且数组下标是从0开始的,所以要小于三

  30. {

  31. for (int j = 0; j < 3; j++) //同理, 只有三列, 所以要小于三.

  32. {

  33. Console.Write(array[i,j] + "\t");

  34. }

  35. Console.WriteLine();

  36. } /*

  37. 打印结果为 1 2 3

  38. * 4 5 6

  39. * 7 8 9

  40. */

  41. // 接下来做一个实例 , 用二维数组打印一个矩阵出来..

  42. string[,] Chess = new string[11, 19]; //声明一个二维数组.

  43. for (int a = 0; a < 11; a++) // 控制行,

  44. {

  45. for (int b = 0; b < 19; b++) //控制列

  46. {

  47. if (b % 2 != 0)

  48. Chess[a, b] = "—";

  49. else

  50. Chess[a, b] = "|";

  51. Console.Write(Chess[a, b]);

  52. }

  53. Console.WriteLine();

  54. }

  55. /* 打印结果

  56. |—|—|—|—|—|—|—|—|—|

  57. |—|—|—|—|—|—|—|—|—|

  58. |—|—|—|—|—|—|—|—|—|

  59. |—|—|—|—|—|—|—|—|—|

  60. |—|—|—|—|—|—|—|—|—|

  61. |—|—|—|—|—|—|—|—|—|

  62. |—|—|—|—|—|—|—|—|—|

  63. |—|—|—|—|—|—|—|—|—|

  64. |—|—|—|—|—|—|—|—|—|

  65. |—|—|—|—|—|—|—|—|—|

  66. |—|—|—|—|—|—|—|—|—|

  67. */

  68. }

  69. }

  70. }


 

========

多维数组(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#

 
  1. class ArrayTest

  2. {

  3. static void Main()

  4. {

  5. // Declare the array of two elements:

  6. int[][] arr = new int[2][];

  7. // Initialize the elements:

  8. arr[0] = new int[5] { 1, 3, 5, 7, 9 };

  9. arr[1] = new int[4] { 2, 4, 6, 8 };

  10. // Display the array elements:

  11. for (int i = 0; i < arr.Length; i++)

  12. {

  13. System.Console.Write("Element({0}): ", i);

  14. for (int j = 0; j < arr[i].Length; j++)

  15. {

  16. System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

  17. }

  18. System.Console.WriteLine();

  19. }

  20. // Keep the console window open in debug mode.

  21. System.Console.WriteLine("Press any key to exit.");

  22. System.Console.ReadKey();

  23. }

  24. }

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值