C#二维和多维数组编程实例

        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]        定义的三维数组;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值