C#基础 数组

数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。

一维数组
            数组初始化,创建数组,数组长度为5
            int [] array=new int[5];
            array[0] = 1;
            array[1] = 2;
            array[2] = 3;
            array[3] = 4;
            array[4] = 5;
            //将2号索引位置改成7
            array[2] = 7;
            //想查看3号索引位置是什么值
            Console.WriteLine(array[3]);
            //把4号索引位置的值赋值给a
            int a = array[4];
            //将1号索引位置的值变成从控制台输入
            array[1] = int.Parse(Console.ReadLine());
            Console.ReadLine();
            直接赋值的数组
            int[] array1 = new int[] {1,2,3,4,5 };

例:存放5个人的人名
            string [] name =new string[5];
            for (int i = 1; i <= 5; i++)
            {
                Console.Write("请输入第"+i+"个人的名字:");
                name[i-1] = Console.ReadLine();
            }
            //查看第三个人的姓名
            //Console.WriteLine(name[2]);
            //将每个人的姓名都打印出来
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(name[i]);
            }
                Console.ReadLine();

例:输入班级人数,输入每个人的分数,求平均分
            Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            double[] fen = new double[a];
            for (int i = 0; i < a; i++)
            {
                Console.Write("请输入第" + (i + 1) + "个人的分数:");
                fen[i] = double.Parse(Console.ReadLine());
            }
            double sum = 0;
            for (int i = 0; i < a; i++)
            {
                sum += fen[i];
            }
            Console.WriteLine("平均分是:" + (sum / a));
            Console.ReadLine();

二维数组
            int [,] shuzu = new int[3,4];
            3     表示有3个一维数组
            4     表示每一个一维数组有4个元素
            //给第一个一维数组的2号索引赋值
            shuzu[0, 2] = 3;

            //直接赋值
            int[,] shuzu1 = new int[,] {
            {1,2,3,4},
            {2,3,4,5},
            {3,4,5,6}
            };

例:根据二维数组,打印一个“王”
            string [,] wang = new string[,]
            {
                {"  ","■","■","■","■","■",""},
                {"  ","  ","  ","■","","",""},
                {"  ","  ","  ","■","","",""},
                {"  ","  ","■","■","■","",""},
                {"  ","  ","  ","■","","",""},
                {"  ","  ","  ","■","","",""},
                {"■","■","■","■","■","■","■"},
            };
            //打印出王
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    Console.Write(wang[i,j]);
                }
                Console.WriteLine();
            }
            Console.ReadLine();

例:输入班级人数,存放每个人的语、数、英成绩进入数组,
            Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            double [,] score = new double [a,3];
            for (int i = 0; i < a; i++)
            {
                Console.Write("请输入第{0}个人的语文成绩:",(i+1));
                score[i, 0] = double.Parse(Console.ReadLine());
                Console.Write("请输入第{0}个人的数学成绩:", (i + 1));
                score[i, 1] = double.Parse(Console.ReadLine());
                Console.Write("请输入第{0}个人的英语成绩:", (i + 1));
                score[i, 2] = double.Parse(Console.ReadLine());
            }
//求每个人的总分
            double [] zong = new double[a];
            for (int i = 0; i < a; i++)
            {
                zong[i] = score[i, 0] + score[i, 1] + score[i, 2];
                Console.WriteLine("第{0}个人的总分是:{1}",(i+1),zong[i]);
            }
//求语文总分      //求数学平均分
            double yuzong = 0;
            double shuzong = 0;
            for (int i = 0; i < a; i++)
            {
                yuzong += score[i, 0];
                shuzong += score[i, 1];
            }
            Console.WriteLine("语文总分是:"+yuzong);
            Console.WriteLine("数学平均分是:"+shuzong/a);
            Console.ReadLine();

 

多维数组

 

数组可以具有多个维度。例如:

 

int[,,] myArray = new  int[2,4,2] {{1,2},{3,4},{5,6},{7,8}} , {{9,10},{11,12},{13,14},{15,16}};

 

冒泡排序

例:从控制台输入班级人数,每个人的年龄放入数组,
      将所有人的年龄求总和
      冒泡排序,求年龄最大
            Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            int[] age = new int[a];
            //int sum = 0;
            for (int i = 0; i < a; i++)
            {
                Console.Write("请输入第{0}个人的年龄:", (i + 1));
                age[i] = int.Parse(Console.ReadLine());
                //sum += age[i];
            }
            //Console.WriteLine("年龄总和是:" + sum);
            //冒泡排序
            for (int i = 0; i < a; i++)
            {
                for (int j = i; j < a - 1; j++)
                {
                    if (age[i] < age[j + 1])
                    {
                        int zhong = age[i];
                        age[i] = age[j + 1];
                        age[j + 1] = zhong;
                    }
                }
            }
            for (int i = 0; i < a; i++)
            {
                Console.WriteLine(age[i]);
            }
                Console.ReadLine();

posted @ 2016-06-26 23:58  Dream&er  阅读(150)  评论(1编辑  收藏  举报