LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#基础:多维数组

admin
2024年8月19日 10:26 本文热度 510

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

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

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


该文章在 2024/8/19 10:26:42 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved