C#基础:多维数组

b2526f417acf699613a9dc616f2786f2.jpeg

如果你有表格、矩阵、网格形状的日期,那么多维数组就很有用。我们通过在方括号中添加逗号来声明多维数组。我们可以以这种方式分别声明二维、三维、四维数组 [,]、[、][、、]。

63fd8ef8dce7ad8114f1fee563774572.jpeg

C 语言中的多维数组#

C# 中带有变量的功能:

一维数组声明:

一维可以通过以下符号来声明。

一维数组声明:示例

dataType[] arrayName;
// Declare an integer array named NumIntArray
int[] NumIntArray;
// Declare a double array named NumDoubleArray
double[] NumDoubleArray;
// Declare a string array named NumStringArray
string[] NumStringArray;

二维数组声明:

二维可以通过以下 sytax 来声明。

二维数组声明:示例

dataType[,] arrayName;
// Later in code, initialize the array with specific values
arrayName = new dataType[rowCount, columnCount];
// A 2D-Array can be declare in following syntax
int[,] 2DIntNumArray = {
{21, 22},
{23, 24},
{25, 26}
};

三维数组声明:

三维可以通过以下符号来声明。

三维数组声明:示例

dataType[,,] arrayName = new dataType[size1, size2, size3];
int[,,] 3IntNumArray = new int[3, 4, 2];

一维数组初始化:

一维可以通过以下 sytax 进行初始化。

一维数组初始化:示例

// Syntax for declaring and initializing an array with specific values
dataType[] arrayName = { value1, value2, ..., valueN };
// Initialize an integer array
int[] 1DIntNumArray = { 1, 2, 3, 4, 5 };
// Initialize a string array
string[] 1DStringArray = { "apple", "banana", "orange" };

二维数组初始化:

二维可以通过以下 sytax 进行初始化。

二维数组初始化:示例

// Syntax for declaring and initializing a two-dimensional array with specific values
dataType[,] arrayName = {
{ value11, value12, ..., value1N },
{ value21, value22, ..., value2N },
// ... additional rows
};
// Declare and initialize a 2D integer array
int[,] 2DIntNumArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

三维数组初始化:

三维可以通过以下 sytax 进行初始化。

三维数组初始化:示例

int[,,] 3DIntOneArray =  
{  
{ {1, 2}, {3, 4}, {5, 6}, {7, 8} },  
{ {9, 10}, {11, 12}, {13, 14}, {15, 16} },  
{ {17, 18}, {19, 20}, {21, 22}, {23, 24} }  
};

一维数组访问:

一维可以通过以下符号来访问。

一维数组访问:示例

dataType[] arrayName;
//Here is syntax of accessing elements
dataType element = arrayName[index];
//Here we declared and initialized an integer array
int[] 1DIntNumArray = { 23, 43, 53, 63, 73 };
//Here is the code of accessing elements
int firstElement = 1DIntNumArray [0]; //Here we are accessing the first element (index 0)
int thirdElement = 1DIntNumArray [2]; //Here we are accessing the third element (index 2)

二维数组访问:

可以通过以下 sytax 来访问二维。

二维数组访问:示例

// A 2D-Array can be declared and initialized in the following way
int[,] 2DIntNumArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// A 2D-Array elements can be accessed in the following way
int element1 = 2DIntNumArray [0, 0]; // Here we are accessing the element at row 0, column 0
int element2 = 2DIntNumArray [1, 2]; // Here we are accessing the element at row 1, column 2

三维阵列访问:

三维数组可以通过以下 sytax 来访问。

三维数组访问:示例

int[,,] 3DIntNumArray = new int[3, 4, 2];
int[,,] 3DElementArray =
{
{ {1, 2}, {3, 4}, {5, 6}, {7, 8} },
{ {9, 10}, {11, 12}, {13, 14}, {15, 16} },
{ {17, 18}, {19, 20}, {21, 22}, {23, 24} }
};
// Here we are accessing elements of the array
int element1 = 3DElementArray [1, 2, 0];
int element2 = 3DElementArray [0, 3, 1];

一维数组迭代:

一维可以通过遵循下面的 sytax 进行迭代。

一维数组迭代:示例

// Here below we have declared and initialized 1D Array
int[] 1DIntNumArray = { 10, 20, 30, 40, 50 };
// Here below we are iterating on loop using 1D array
for (int i = 0; i < 1DIntNumArray.Length; i++)
{
  Console.WriteLine("Element Of One Dimensional Array at index " + i + ": " + 1DIntNumArray[i]);
}

二维数组迭代:

A 二维可以通过遵循下面的 sytax 进行迭代。

二维数组迭代:示例

// Here we are declared and initialized 2D Array
int[,] 2DIntNumArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
//Here we are iterating on 2D array using nested for loop
for (int i = 0; i < 2DIntNumArray.GetLength(0); i++)
{
for (int j = 0; j < 2DIntNumArray.GetLength(1); j++)
{
Console.Write(2DIntNumArray[i, j] + " ");
}
Console.WriteLine(); // Move to the next row
}

三维数组迭代:

A 三维可以通过遵循下面的 sytax 进行迭代。

三维数组迭代:示例

int[,,] 3DIntNumArray = new int[3, 4, 2];
// Here we accessing element of 3D Array
for (int s = 0; s < 3; s++)
{
  for (int y = 0; y < 4; y++)
  {
    for (int m = 0; m < 2; m++)
    {
      3DIntNumArray[s, y, m] = s + y + m;
    }
  }
}

一维数组修改:

一维可以通过遵循下面的 sytax 进行修改。

一维数组修改:示例

// Here we declared and initialized 1D Array
int[] 1DIntNumArray = { 1, 2, 3, 4, 5 };
// Here we are modifying element at index 2 of 1D array
1DIntNumArray[2] = 35;

二维阵列修改:

二维可以通过以下符号进行修改。

二维数组修改:示例

// Here we have declared and initialized 2D array
int[,] 2DIntNumDArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
2DIntNumDArray[1, 2] = 10;

三维阵列修改:

A 三维可以通过以下符号进行修改。

三维数组修改:示例

int[,,] 3DIntNumArray = new int[3, 4, 2];
int[,,] 3DIntNumArray =
{
{ {1, 2}, {3, 4}, {5, 6}, {7, 8} },
{ {9, 10}, {11, 12}, {13, 14}, {15, 16} },
{ {17, 18}, {19, 20}, {21, 22}, {23, 24} }
};
//Here we are modifying an element in the array
3DIntNumArray[1, 2, 0] = 999;

一维数组长度:

一维数组的长度可以通过遵循下面的 sytax 来获得。

一维数组长度:示例

// Here we declared and initialized 1D Array  
int[] 1DIntNumArray = { 10, 20, 30, 40, 50 };  
// Here we are getting the size of 1D Array  
int size = 1DIntNumArray.Length;

二维数组长度:

二维数组的长度可以通过遵循下面的 sytax 来获得。

二维数组长度:示例

// Here we have declared and initialized 2D Array  
int[,] 2DIntNumArray = {  
{1, 2, 3},  
{4, 5, 6},  
{7, 8, 9}  
};  
// Here are are getting the size of 2D Array With rows and column  
int numRows = 2DIntNumArray.GetLength(0); // Number of rows  
int numCols = 2DIntNumArray.GetLength(1); // Number of column

三维阵列长度:

三维数组的长度可以通过遵循下面的 sytax 来获得。

三维数组长度:示例

int[,,] 3DIntNumArray = new int[3, 4, 2];  
int[,,] 3DIntNumArray =  
{  
{ {1, 2}, {3, 4}, {5, 6}, {7, 8} },  
{ {9, 10}, {11, 12}, {13, 14}, {15, 16} },  
{ {17, 18}, {19, 20}, {21, 22}, {23, 24} }  
};  
3DIntNumArray.GetLength(0);  
3DIntNumArray.GetLength(1)  
3DIntNumArray.GetLength(2)

C# 中的多维数组与示例

using System;  
public class Program  
{  
  public static void Main(string[] args)  
  {  
    int[,] mangoPrices = { {10, 11 ,12}, {20, 21,22}, {30, 31,32} }; //declaration and initialization  
    for (int i = 0; i < 3; i++)  
    {  
      for (int j = 0; j < 3; j++)  
      {  
        Console.Write(mangoPrices[i,j]+" ");  
      }  
      Console.WriteLine();//new line at each row  
    }  
  }  
}

输出

10 11 12  
20 21 22  
30 31 32

如果你正在制作程序、游戏、软件或矩阵进行数学运算,那么你可以使用多维数组

如果你喜欢我的文章,请给我一个赞!谢谢

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值