C

程序员有二十年 2024-08-19 10:28:35

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

C 语言中的多维数组#

C# 中带有变量的功能:一维数组声明:

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

一维数组声明:示例

dataType[] arrayName;// Declare an integer array named NumIntArrayint[] NumIntArray;// Declare a double array named NumDoubleArraydouble[] NumDoubleArray;// Declare a string array named NumStringArraystring[] NumStringArray;二维数组声明:

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

二维数组声明:示例

dataType[,] arrayName;// Later in code, initialize the array with specific valuesarrayName = new dataType[rowCount, columnCount];// A 2D-Array can be declare in following syntaxint[,] 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 valuesdataType[] arrayName = { value1, value2, ..., valueN };// Initialize an integer arrayint[] 1DIntNumArray = { 1, 2, 3, 4, 5 };// Initialize a string arraystring[] 1DStringArray = { "apple", "banana", "orange" };二维数组初始化:

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

二维数组初始化:示例

// Syntax for declaring and initializing a two-dimensional array with specific valuesdataType[,] arrayName = {{ value11, value12, ..., value1N },{ value21, value22, ..., value2N },// ... additional rows};// Declare and initialize a 2D integer arrayint[,] 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 elementsdataType element = arrayName[index];//Here we declared and initialized an integer arrayint[] 1DIntNumArray = { 23, 43, 53, 63, 73 };//Here is the code of accessing elementsint 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 wayint[,] 2DIntNumArray = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};// A 2D-Array elements can be accessed in the following wayint element1 = 2DIntNumArray [0, 0]; // Here we are accessing the element at row 0, column 0int 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 arrayint element1 = 3DElementArray [1, 2, 0];int element2 = 3DElementArray [0, 3, 1];一维数组迭代:

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

一维数组迭代:示例// Here below we have declared and initialized 1D Arrayint[] 1DIntNumArray = { 10, 20, 30, 40, 50 };// Here below we are iterating on loop using 1D arrayfor (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 Arrayint[,] 2DIntNumArray = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};//Here we are iterating on 2D array using nested for loopfor (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 Arrayfor (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 Arrayint[] 1DIntNumArray = { 1, 2, 3, 4, 5 };// Here we are modifying element at index 2 of 1D array1DIntNumArray[2] = 35;二维阵列修改:

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

二维数组修改:示例

// Here we have declared and initialized 2D arrayint[,] 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 array3DIntNumArray[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 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

程序员有二十年

简介:感谢大家的关注