如何在 C# 中初始化矩形数组?
数组用于存储数据集合,但通常会认为数组是同一类型的变量集合,这些变量存储在连续的内存位置中。
多维数组也称为矩形数组。多维数组通过为每行指定括号值来初始化。
以下数组有 2 行,每行有 2 列。
int [,] a = new int [2,2] {
{20, 50} , /* initializers for row indexed by 0 */
{15, 45} , /* initializers for row indexed by 1 */
};我们来看一个示例 -
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
/* an array with 2 rows and 2 columns*/
int [,] a = new int [2,2] {
{20, 50} , /* initializers for row indexed by 0 */
{15, 45} , /* initializers for row indexed by 1 */
};
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
}
}
Console.ReadKey();
}
}
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP