如何在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();
}
}
}热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿