数组

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

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

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;

posted @ 2016-03-17 01:21  颠覆整个世界  阅读(131)  评论(0编辑  收藏  举报