数组,一维,二维,多维

Posted 柴亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组,一维,二维,多维相关的知识,希望对你有一定的参考价值。

数组

 

一维数组

         数组初始化,创建数组,数组长度为5

            int [] array=new int[5];

            array[0] = 1;

            array[1] = 2;

            array[2] = 3;

            array[3] = 4;

            array[4] = 5;

         直接赋值的数组

            int[] array1 = new int[] {1,2,3,4,5 };

数组,在创建的时候,需要规定数据类型,初始长度

 

 

二维数组

            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}

            };

 

冒泡排序

           for (int i = 0; i < 10;i++ )

            {

                for (int j =i; j < 9;j++ )

                {

                    if(shuzi[i]<shuzi[j+1])

                    {

                       int zhong = shuzi[i];

                       shuzi[i] = shuzi[j + 1];

                       shuzi[j + 1] = zhong;                 

                    }

                }

练习:

输入人数,输入每个人年龄,求总和

 

            // Console.Write("请输入班级人数:");

            // int n = int.Parse(Console.ReadLine());

            // int [] ban=new int[n];

            //int sum=0;

            // for (int i = 0; i < n; i++)

            // {

            //     Console.Write("请输入年龄:");

            //    ban [i]=int.Parse(Console.ReadLine());

            //    sum += ban[i];

            // }

            // Console.WriteLine("年龄的总和是"+sum);

            // Console.ReadLine();

 输入班级人数,存放语数英成绩,求每人总和,语文总分,数学平均分

            //Console.Write("请输入班级人数:");

            //int n = int.Parse(Console.ReadLine());

            //double[,] chengji = new double[n, 3];

            //double a = 0;

            //double yz = 0;

            //double sp = 0;

            //for (int i = 0; i < n; i++)

            //{

            //    Console.Write("请输入语文成绩:");

            //    chengji[i, 0] = double.Parse(Console.ReadLine());

            //    Console.Write("请输入数学成绩:");

            //    chengji[i, 1] = double.Parse(Console.ReadLine());

            //    Console.Write("请输入英语成绩:");

            //    chengji[i, 2] = double.Parse(Console.ReadLine());

            //}

            //for (int j = 0; j < n; j++)

            //{

            //    a = chengji[j, 0] + chengji[j, 1] + chengji[j, 2];

            //    Console.WriteLine("第{0}个人的三门成绩总和是{1}", (j + 1), a);

            //}

            //for (int k = 0; k < n; k++)

            //{

            //    yz += chengji[k, 0];

            //}

            //Console.WriteLine("语文成绩的总和是:" + yz);

            //for (int p = 0; p < n; p++)

            //{

            //    sp += chengji[p, 1];

            //}

            //Console.WriteLine("数学平均分成绩的是:" + (sp / n));

            //Console.ReadLine();

 

 

           

打印柴,利用二维数组

            //string[,] mingzi = new string[16, 10]

            //{

            //{"  ","  ","  ","●","  ","  ","","●","",""},

            //{"  ","  ","  ","●","  ","  ","●","  ","  ",""},

            //{"  ","●","  ","●","  ","  ","●","  ","  ","●"},

            //{"  ","●","  ","●","  ","  ","●","  ","●",""},

            //{"  ","●","  ","●","●","●","●","●","",""},

            //{"  ","●","  ","●","  ","  ","●","","",""},

            //{"  ","●","  ","●","  ","  ","●","","","●"},

            //{"●","●","●","●","●","  ","●","●","●",""},

            //{"  ","  ","  ","  ","●","","","","",""},

            //{"  ","  ","  ","  ","●","","","","",""},

            //{"  ","●","●","●","●","●","●","●","●",""},

            //{"  ","  ","  ","●","●","●","","","",""},

            //{"  ","  ","●","  ","●","  ","●","","",""},

            //{"  ","●","  ","  ","●","  ","  ","●","",""},

            //{"●","  ","  ","  ","●","  ","  ","  ","●",""},

            //{"  ","  ","  ","  ","●","  ","  ","  ","  ",""},

            //};

            //for(int i=0;i<16;i++)

            //{

            //for(int b=0;b<10;b++)

            //{

            //Console.Write(mingzi[i,b]);

            //}

            //Console.WriteLine();

            //}

            //Console.ReadLine();

随机一百以内10个数,按照大小顺序排列

 

            int[] shuz = new int[10];

            Random shu = new Random();

            for (int i = 0; i < 10; i++)

            {

                int a = shu.Next(101);

                shuz[i] = a;

                Console.WriteLine(shuz[i]);

            }

            for (int j = 0; j < 10; j++)

            {

                for (int k = j; k < 9; k++)

                {

                    if (shuz[j] > shuz[k + 1])

                    {

                        int zhong = shuz[j];

                        shuz[j] = shuz[k + 1];

                        shuz[k + 1] = zhong;

                    }

                }

            }

            for (int i = 0; i < 10; i++)

            {

                Console.WriteLine(shuz[i]);

            }

            Console.ReadLine();

 

随机一百以内十个不重复的数,按照大小顺序排列。

           // int[] shuz = new int[10];

           //Random shu=new Random();

           // for (int i = 0; i < 10; i++)

           // {

           //    bool b=true;

           //     int a = shu.Next(101);

           //     shuz[i] = a;

           //     for (int j = 0; j < i; j++)

           //     {

           //         if (a == shuz[j])

           //         {

           //             b = false;

           //             i--;

           //         }

           //         if (b)

           //         {

           //             shuz[i] = a;

           //         }

           //     }

           //     Console.WriteLine(shuz[i]);

           // }

           // for (int j = 0; j < 10; j++)

           // {

           //     for (int k = j; k < 9; k++)

           //     {

           //         if (shuz[j] > shuz[k + 1])

           //         {

           //             int zhong = shuz[j];

           //             shuz[j] = shuz[k + 1];

           //             shuz[k + 1] = zhong;

           //         }

           //     }

           // }

           // for (int i = 0; i < 10; i++)

           // {

           //     Console.WriteLine(shuz[i]);

           // }

           // Console.ReadLine();

            //Random ran = new Random();

    //输入每个人的手机号,,进行滚动显示,,清屏,随即一个索引,根据索引打印,等待0.1秒

           // Console.Write("请输入人数:");

           // int a =int.Parse( Console.ReadLine());

           // string[]zhj=new string[a];

           // for (int i = 0; i < a;i++ )

           // {

           //     Console.Write("请输入第{0}个人的手机号:",(i+1));

           //     zhj[i] = Console.ReadLine();

           // }

           // Console.WriteLine("所有号码输入完毕,请按回车继续抽奖活动!");

           //Console.ReadLine();

           // Random ran=new Random();

           // for (int j = 0; j < 30;j++ )

           // {

           //     Console.Clear();

           //     int aa = ran.Next(a);

           //     Console.WriteLine("中奖号码是:"+zhj[aa]);

           //     System.Threading.Thread.Sleep(100);

           // }

           // Console.ReadLine();

以上是关于数组,一维,二维,多维的主要内容,如果未能解决你的问题,请参考以下文章

php 一个多维数组根据一维数组筛选数据,得到新数组

一维数组复习,二维数组与多维数组

数组,一维,二维,多维

多维数组与指针

thinkphp怎么将多维数组变成一维数组

php中的二维多维数组到一维数组[重复]