数组的学习

Posted 请叫我魔术先生

tags:

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

一维数组[相同数据类型]

第一种定义方式    一般使用第一种。

int[] shuzu=new int[5];

for (int i=0;i<5;i++)
{
  shuzu[i]=i+1;
}

索引是从零开始的。

赋值:

            shuzu[0] = 1;//索引号
            shuzu[1] = 2;
            shuzu[2] = 3;
            shuzu[3] = 4;
            shuzu[4] = 5;
            //shuzu[5]=6;
            Console.WriteLine();

第二种定义方式:

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

例:

从控制台输入10个人的年龄放入数组,将10个人的年龄求总和

            int[] age = new int[10];
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                age[i] = int.Parse(Console.ReadLine());
                sum += age[i];
            }
            Console.WriteLine(sum);

例:

输入班级人数,根据人数,挨个输入成绩,求平均分。

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

二维数组:

int[,] shuzu = new int[3, 4];   //3个一维数组  每个里面有四个值。

/string[,] shuzu = new string[,]{};

例:

输入班级人数,将每个人的语文、数学、英语成绩输入二维数组

Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            double[,] fen = new double[a, 3];
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (j == 0)
                    {
                        Console.Write("请输入第" + (i + 1) + "人的语文成绩:");
                    }
                    if (j == 1)
                    {
                        Console.Write("请输入第" + (i + 1) + "人的数学成绩:");
                    }
                    if (j == 2)
                    {
                        Console.Write("请输入第" + (i + 1) + "人的英语成绩:");
                    }
                    fen[i, j] = double.Parse(Console.ReadLine());
                }
            }

8行7列  企

            string[,] shuzu = new string[8, 7]{           
                {"  ","  ","  ","","","","" },
                {"  ","  ","","  ","","","" },
                {"  ","","  ","","  ","","" } ,
                {"","  ","  ","","  ","  ","" },
                {"  ","  ","  ","","","",""},
                {"  ","","  ","","","","" }, 
                {"  ","","  ","","","","" },
                {"","","","","","","" },
            };

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    Console.Write(shuzu[i, j]);

                }
                Console.WriteLine();
            }

技术分享

 

foreach(int a in shuzu)//遍历数组

冒泡排序:从大到小

            int[] shuzu = new int[] { 3, 1, 8, 4, 0, 5, 6 };
            //8134056
            //8314056
            //8413056
            //8513046
            //8613045
            //8631045
            //8641035
            //8651034
            //8653014
            //8654013
            //8654103
            //8654301
            //8654310
            int zhong;
            for (int i = 0; i < 7; i++)
            {
                for (int j = i; j < 6; j++)
                {
                    if (shuzu[i] < shuzu[j + 1])
                    {
                        zhong = shuzu[i];
                        shuzu[i] = shuzu[j + 1];
                        shuzu[j + 1] = zhong;
                    }
                }
            }
            //for (int i = 0; i < 7; i++)
            //{
            //    Console.WriteLine(shuzu[i]);
            //}

            foreach (int a in shuzu)//遍历数组
            {
                Console.WriteLine(a);
            }

object所有类的基类,可以接受 任何数组类型

object a = 1;
object b = "123";
object c = true;
object d = 3.14;

 

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

VSCode自定义代码片段10—— 数组的响应式方法

web代码片段

错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”

javascript常用代码片段

为什么我不能在此片段中生成唯一对象数组?

IOS开发-OC学习-常用功能代码片段整理