zoukankan      html  css  js  c++  java
  • 数组

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

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

    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;

  • 相关阅读:
    渡一 5-1、2 条件语句补充,初识引用值,typeof,类型转换
    渡一 3-2 js基础知识
    渡一 25 正则表达式
    渡一 15-1 数组去重(上下)
    渡一 14-3 数组,类数组
    渡一 14-1&2 三目运算符,数组,类数组
    3_Guess Fingers
    2_Cat Years
    27_Blog Reader
    26_Json_Example
  • 原文地址:https://www.cnblogs.com/dianfu123/p/5285880.html
Copyright © 2011-2022 走看看