如何在C#中声明2D整数数组

本文关键字:2D 整数 数组 声明 | 更新日期: 2023-09-27 17:53:53

我正处于编程的初级阶段,我想知道如何在C#中声明二维数组。我确实在谷歌上查过,但找不到解决方案。

请以我的水平回答这个问题。

感谢

如何在C#中声明2D整数数组

您可以这样做。点击此处查看详细信息

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

2D整数数组

Declaration

int[,] array = new int[4, 2];

Initialization

int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

完整解释并举例说明:http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

//二维数组。

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

//指定了维度的同一数组。

int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

//具有字符串元素的类似数组。

string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                    { "five", "six" } };

//三维阵列。

int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                             { { 7, 8, 9 }, { 10, 11, 12 } } };

//指定了维度的同一数组。

int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                   { { 7, 8, 9 }, { 10, 11, 12 } } };

我想你还没有搜索过谷歌。。。。

按照下面的链接查看教程http://www.tutorialspoint.com/csharp/csharp_multi_dimensional_arrays.htm

int [,] a = int [3,4] = {  
 {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
 {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
 {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

将MSDN用于Micrsoft技术,这是有据可查的http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

当我写:2d整数数组c#时,它在谷歌搜索中排名第一

这个页面还可能提供有用的信息:多维数组和C#中的数组之间有什么区别?

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

sum2D((方法

private double sum2D(double[,] ar)
{
 double sum = 0.0;
 foreach (double d in ar)
      sum += d;
 return sum;
}