如何在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();
}
}
}热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短