C#基础知识学习——数组与动态数组(初始化、方法)、字符串 (三)

数组介绍

c#中数据类型分为值类型与引用类型,数组属于引用类型,并且索引号从零开始。
值类型的变量基本都是在栈中分配空间(栈(连续分配):先进后出,所以程序处理中会有压栈过程,即程序执行过程)
引用类型的实例化后基本都是在堆中分配空间(堆(数据不是连续分配、有内存碎片)),数组是连续的内存空间

数组初始化与访问

实例化数组的时候就需要指定容量,大小不能更改;数据类型[] 数组名称=new 数据类型[数据容量]

//元素未知数组定义
int[] arr=new int[5];
//元素已知数组定义
int[] ass={1,2,1,8}int[] ass =new int[] { 1, 2, 1, 8 };int[4] ass =new int[] { 1, 2, 1, 8 };
//按照索引号(下标)访问--注意访问时下标越界问题
int b=ass[0]; 
//随机产生10个分数并保存到数组
            //随机数对象
            Random random = new Random();          
            int[] score = new int[10]; 
            for (int i = 0; i < 10; i++)
            {
                score[i] = random.Next(101);
            }
            //读取数组,foreach遍历循环的循环变量是只读的,不能进行赋值
            foreach (var sc in score)
            {
                Console.WriteLine("成绩:{0}分",sc);
            }
//随机产生10个分数并保存到数组,求平均分、最高分
            //随机数对象
            Random random = new Random();
            int[] score = new int[10];
            int sum1 = 0;
            int sum2 = 0;
            for (int i = 0; i < 10; i++)
            {
                score[i] = random.Next(101);
                sum1 += score[i];
            }
            //读取数组,foreach遍历循环的循环变量是只读的,不能进行赋值
            foreach (var sc in score)
            {
                sum2 += score[i];
                Console.WriteLine("成绩:{0}分", sc);
            }
            Console.WriteLine("sum1={0}\nsum2={1}", sum1, sum2);
            Console.WriteLine("平均分:{0}分", sum1 / score.Length);
            int max = 0;
            for (int i = 0; i < score.Length; i++)
            {
                if (score[i] > max)
                {
                    max = score[i];
                }
            }
            Console.WriteLine("人工获取最高分:{0}分", max);

            //Array提供一些方法,用于创建、处理、搜索数组,并对数组进行排序
            //Array.Sort对数组进行升序排序(小<大)
            Array.Sort(score);
            max = score[score.Length-1];
            Console.WriteLine("自动获取最高分:{0}分",max);
            //对数据元素进行倒序排序(大<小)
            Array.Reverse(score);

一维数组用法

冒泡算法

对一维数组进行顺序或者倒序排序:对相邻两个数据进行对比与换位
用双循环进行解决:外循环控制矩阵数量,内循环控制比较
C#基础知识学习——数组与动态数组(初始化、方法)、字符串 (三)_第1张图片

 //双循环
            int[] Data = { 8, 7, 5, 1, 6 };
            for (int i = 0; i < Data.Length-1; i++)
            {
                for (int j = 0; j < Data.Length - 1-i; j++)
                {
                    if (Data[j] > Data[j + 1])
                    {
                        int change = Data[j];
                        Data[j] = Data[j + 1];
                        Data[j + 1]= change;
                    }
                }
            }
//打印乘法表
            int[] Data = { 8, 7, 5, 1, 6 };
            for (int i = 0; i <= 9; i++)
            {
                for (int j = 0; j <=i; j++)
                {
                    Console.Write(i + "×" + j + "=" + (i * j)+" ");
                }
                Console.WriteLine("");
            }

交错数组

数组元素本身也是一个数组

定义

          int[][] a = new int[5][];
            a[0] = new int[8];
            a[1] = new int[8];
            a[2] = new int[8];
            a[3] = new int[8];
            a[4] = new int[8];
            //a[0] = new int[] { 10, 20, 30, 80, 100 };
            //a[1] = new int[] { 10, 20, 30, 80,90 };
            Random random = new Random();
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j <8; j++)
                {
                    a[i][j] = random.Next(1,50);
                }
            }
            //打印数组
            foreach (var item in a)
            {
                foreach (var c in item)
                {
                    Console.Write("{0},",c);
                }
                Console.WriteLine();
            }
            //数组维度
            int rnk=a.Rank;

//锯齿状数组
           int[][] jichi = new int[][]
           {
                new int[] { 1, 2 },
            new int[] { 1, 2 ,5},
            new int[] { 1, 2,8,0,2 },
            new int[] { 1, 2 ,7,10,848,},
           };

            foreach (int[] item in jichi)
            {
                foreach (int items in item)
                {
                    Console.Write(items + " ");
                }
                Console.WriteLine();
            }
             int[][] Jarr = new int[5][];
            //赋值,长度可以不一致
            Jarr[0] = new int[] { 8,7,5,9,2};
            Jarr[1] = new int[] { 8, 9, 0 };
            int[][] Jarri = new int[4][];
            Jarri[0] = new int[8];
            Jarri[1] = new int[2];
            Jarri[2] = new int[8];
            Jarri[3] = new int[8];
            Random random = new Random();
            for (int i = 0; i < Jarri.Length; i++)
            {
                for (int j = 0; j < Jarri[i].Length; j++)
                {
                    Jarri[i][j] = random.Next(100);
                }
            }
            foreach (int[]a in Jarri)
            {
                foreach (int b in a)
                {
                    Console.Write("{0}  ",b);
                }
                Console.WriteLine();
            }

二维数组

           //创建一个随机对象
           Random random = new Random();
            //初始化
            int[,] a = new int[3, 3];
            //赋值
            a[0,1] = 10;
            //数组行
            int row=a.GetLength(0);
            //数组列
            int lie = a.GetLength(1);
            //正对角线之和
            int s1=0;
            //负对角线之和
            int s2=0;
            //矩阵赋值
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                //random取值为[0,20)
                    a[i, j] = random.Next(20);
                    Console.Write("{0}  ", a[i, j]);
                    if (i==j)
                    {
                        s1 += a[i, j];
                    }
                    if (i+j==a.GetLength(0)-1)
                    {
                        s2 += a[i, j];
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("正对角线之和{0},副对角线之和{1}",s1,s2);

数组方法

c#定义一个数组都会继承System.Array

//数组字符串排序--按照首字母排序
            string[] s = new string[] { "sfbh", "gerg", "igrerh" };
            string[] c = new string[5];
            c[0] = "dgaeha";
            Array.Sort(s);
            foreach (string item in s)
            {
                Console.Write(item+" ");
            }
            Console.WriteLine();
            //数组反转字符串
            Array.Reverse(s);
            
            foreach (string item in s)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            //数组与数组之间复制
            Array.Copy(s, 0, c, 1, s.Length);
            foreach (string item in c)
            {
                Console.Write(item + " ");
            }

动态数组

 //创建
            ArrayList arrayList = new ArrayList();
            //创建并初始化
            ArrayList arrayList0 = new ArrayList { "geerh",78,78.8995};
            //添加元素
            arrayList.Add(1M);//decimal数据
            arrayList.Add(56.78);//double数据
            arrayList.Add(true);//bool数据
            arrayList.Add("dfbgab");
            arrayList.Add("dfbgab");
            arrayList.Add("dfbgab");
            arrayList.Add("dfbgab");
            //放入一个集合
            arrayList.AddRange(arrayList0);
            //获取元素
            Console.WriteLine(arrayList[0]);
            //数据修改
            arrayList[2] = 78888;
            Console.WriteLine(arrayList[2]);
            foreach (var item in arrayList)
            {
                Console.Write(item+" ");
            }
            Console.WriteLine();
            //数据删除
            //就近移除,不会继续删除
            arrayList.Remove("dfbgab");
            arrayList.RemoveAt(0);
            arrayList.RemoveRange(0, 1);
            foreach (var item in arrayList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            arrayList.Reverse();
            foreach (var item in arrayList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            //集合转数组
            object[] shuzu = arrayList.ToArray();
            Console.WriteLine(shuzu.Length);

字符串

字符串由字符组成,即字符串也是一个字符数组

            string s = "fgearh";
            char a = s[2];
            Console.WriteLine(a);
            string astring = "abc";
            String bstring = new string(new char[] { 'a','b','c'});
            //字符串比较
            int d = astring.CompareTo(bstring);
            Console.WriteLine(d);
            //字符串大小写变换
            astring = astring.ToLower();
            Console.WriteLine(astring);
            astring = astring.ToUpper();
            d = astring.CompareTo(bstring);
            Console.WriteLine(astring);
            Console.WriteLine(d);
            //替代
            astring = astring.Replace("AB","op");
            Console.WriteLine(astring);
            //包含
            bool result = astring.Contains("op");
            Console.WriteLine(result);

你可能感兴趣的:(c#基础,c#,学习,开发语言)