首页 > 代码库 > 8、二维数组

8、二维数组

1、多维数组:①二维数组:表示有多个一维数组②三维数组:表示有多个二维数组2、定义数组:①定义二维数组int[,] 数组名 = new int[5, 5]表示定义一个5个长度为5的一维数组。②定义三维数组int[,,] 数组名 = new int[5, 5,5]表示定义一个5个长度为[5, 5]的二维数组。3、赋值①向一维数组中赋值:int[] 数组名 = new int[3]{2,4,6};②向二维数组中赋值:int[,] 数组名 = new int[33]{{1,2,3},{4,5,6},{7,8,9}}4、二维数组求值输入人数,以及姓名、年龄、身高,求平均年龄,按身高将序排列:            Console.WriteLine("请输入人数:");            int renshu = int.Parse(Console.ReadLine());            string[,] a = new string[renshu, 3];            int[,] b = new int[renshu, 3];            for (int m = 0; m < renshu; m++)            {                for (int n = 0; n < 3; n++)                {                    if (n == 0)                    {                        Console.WriteLine("请输入第{0}个人的姓名:", m + 1);                        a[m, n] = Console.ReadLine();                    }                    if (n == 1)                    {                        Console.WriteLine("请输入第{0}个人的年龄:", m + 1);                        a[m, n] = Console.ReadLine();                    }                    if (n == 2)                    {                        Console.WriteLine("请输入第{0}个人的身高:", m + 1);                        a[m, n] = Console.ReadLine();                    }                }            }            decimal avg, sum = 0;            for (int m = 0; m < renshu; m++)            {                sum = int.Parse(a[m, 1]) + sum;            }            avg = sum / renshu;            Console.WriteLine("平均年龄为:{0}", avg);            for (int m = 0; m < renshu - 1; m++)            {                for (int n = m + 1; n < renshu; n++)                {                    if (int.Parse(a[m, 2]) > int.Parse(a[n, 2]))                    {                        string zhong1;                        zhong1 = a[m, 0];                        a[m, 0] = a[n, 0];                        a[n, 0] = zhong1;                        string zhong2;                        zhong2 = a[m, 1];                        a[m, 1] = a[n, 1];                        a[n, 1] = zhong2;                        string zhong3;                        zhong3 = a[m, 2];                        a[m, 2] = a[n, 2];                        a[n, 2] = zhong3;                    }                }            }            Console.WriteLine("按身高将序排列为:(姓名 年龄 身高)");            for (int m = 0; m < renshu; m++)            {                Console.Write("身高第{0}的是:", m + 1);                for (int n = 0; n < 3; n++)                {                    Console.Write(a[m, n] + " ");                }                Console.Write("\n");            }

 

8、二维数组