3.数组的使用

static void Main(string[] args)
{
//数组使用的基本步骤
//[1]声明数组
int[] scores;
//[2]分配空间
scores = new int[5];
//[3]赋值
scores[1] = 4;
//[4]处理数据
scores[1] = scores[1] + 7;
scores[2] = scores[2] + 3;

        //声明数组的几种方法
        int[] score1 = new int[] { 1, 3, 7 };
        string[] score2 = new string[] { "", "", "" };
        float[] score3 = { 12, 3, 7 };

        //数组的长度
        int corelength = score3.Length;

        //使用for循环读取数组的元素
        float sum1 = 0;
        for (int i = 0; i < score3.Length; i++)
        {
            sum1 += score3[i];
            Console.WriteLine(score3[i]);
        }

        Console.WriteLine("- --------------------");

        //使用foreach遍历数组
        int num = 0;
        foreach (int item in score1)
        {
            num += item;
        }

        //使用逗号分开数组
        string name1 = "小王,小江,小刘";
        string[] name1List = name1.Split(',');

        //使分割后的字符串使用下划线;连在一起
        string name2 = " x i a o w a n g ";
        string[] name2List = name2.Split();
        name2 = string.Join("_", name2List);





        Console.Read();
    }
posted on 2020-12-29 17:54  cq752522131  阅读(41)  评论(0)    收藏  举报