void Button1Click(object sender, EventArgs e)
{
/* 一个带有 4 行 3 列的数组 */
int[,] a = new int[4, 3] {{70,75,90}, {91,92,77}, {82,84,98}, {93,76,85}};
int i, j;
/* 输出数组中每个元素的值 */
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
//Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
textBox1.Text = textBox1.Text + a[i,j] + ", ";
}
textBox1.Text = textBox1.Text + Environment.NewLine;
}
}
void Button2Click(object sender, EventArgs e)
{
int[, ,] muarr = new int[2, 2, 3]
{
{{1,2,3},{4,5,6}},
{{7,8,9},{2,3,4}}
};
int rank = muarr.Rank;
textBox2.Text = textBox2.Text + "该多维数组的维数为:"+rank + Environment.NewLine;
int rlength = muarr.GetLength(1);
textBox2.Text = textBox2.Text + "该多维数组的第二维有" + rlength + "个元素" + Environment.NewLine;
textBox2.Text = textBox2.Text + "开始遍历多维数组" + Environment.NewLine;
textBox2.Text = textBox2.Text + Environment.NewLine;
textBox2.Text = textBox2.Text + "----------------------------------" + Environment.NewLine;
textBox2.Text = textBox2.Text + Environment.NewLine;
for (int i = 0; i < muarr.GetLength(0);i++ )
{
for (int js1 = 0; js1< muarr.GetLength(1); js1++)
{
for(int js2 = 0;js2<muarr.GetLength(2);js2++)
{
textBox2.Text = textBox2.Text + muarr[i,js1,js2] + ", ";
}
textBox2.Text = textBox2.Text + Environment.NewLine;
}
textBox2.Text = textBox2.Text + Environment.NewLine;
}
}
运行如下;
int[,] a = new int[4, 3] 定义的二维数组;
int[, ,] muarr = new int[2, 2, 3] 定义的三维数组;