c#中的数据类型简介(数组)

c#中的数据类型简介(数组)

数组定义

  • 可以将数组看成相同数据类型的一组或多组数据,包括一维数组,多维数组和交错数组。
  • 数值数组元素的默认值设置为零,而引用元素的默认值设置为 null。 
  • 交错数组是指元素为数组的数组,因此,它的元素是引用类型,初始化为 null。 
  • 数组的索引从零开始:具有 n 个元素的数组的索引是从 0 到 n-1。

数据申明

一维数组的几种申明和初始化

type[]  typeName = new type[n];                                                //定义数组但是未赋值

type[0] = item1;

type[1] = item2;

type[2] = item3; ......

type[n-1] =itemn;

type[]  typeName = new type[n] { item1,item2,item3,......itemn};//系统可以自动推算数组长度

type[]  typeName = new type[] { item1,item2,item3,......itemn};  //系统可以自动推算数组长度

type[]  typeNmae = {item1,item2,item3,......itemn};      //省略赋值的数据类型,系统可以自动推断,最简洁的定义方式

多维数组和交错数组的申明和初始化与一维数组类似,请看以下示例程序:

    class Program
    {
        static void Main(string[] args)
        {
            int[] intA = new int[5];  
            intA[0] = 1;
            intA[1] = 2;
            intA[2] = 3;
            intA[3] = 4;
            intA[4] = 5;
            int[] intB = new int[] { 1, 2, 3, 4, 5 };
            int[] intC = new int[5] { 1, 2, 3, 4, 5 };
            int[] intD = { 1, 2, 3, 4, 5 };

            //数组的取值,返回索引位3的值,返回的值为3
            int item3 = intA[2];
            //int[]的抽象基类System.Array,继承了IEnumerable接口,可以使用foreach遍历数组成员
            foreach (int item in intA)
                Console.WriteLine("item is:{0}", item);
            //可以使用for循环访问数组成员
            for (int i = 0; i < intA.Length; i++)
                Console.WriteLine("each is :{0}", intA[i]);
            //表明数组成员可以被赋值
            bool b1 = intA.IsReadOnly;     //返回false
            bool b2 = intA.IsSynchronized; //返回false,可以异步访问
            //数组一经定义好后,是不能被新增、插入、删除的
            bool b3 = intA.IsFixedSize;    //返回true,表示数组是固定长度的

            //多维数组申明和初始化
            string[,] strA = new string[3,2]{ { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } };
            string[,] strB = new string[,] { { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } };
            string[,] strC = { { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } };
            string[,] strD = new string[3, 2];
            strD[0, 0] = "GZ"; strD[0, 1] = "SZ";
            //多维数组的取值
            string item11 = strA[1, 1];
            string itemX = strD[2,1]; //初始化未赋值,其默认值为null
            //多维数组成员foreach遍历
            foreach (string item in strA)
                Console.WriteLine("foreach遍历:{0}",item);
            //多维度成员的for遍历,w1和w2用来计算各维度的元素个数,也可用getLength方法获取
            int w1 = strA.GetUpperBound(1)+1;
            int w2 = strA.GetUpperBound(0)+1;
            for (int i = 0; i < w2; i++)
                for (int j = 0; j < w1; j++)
                    Console.WriteLine("for遍历:{0}",strA[i, j]);
            
            //交错数组,表示成员为数组的数组
            int[][] arry = new int[2][];
            arry[0] = new int[3] { 6, 7, 8 };
            arry[1] = new int[4] { 9, 4, 5, 6 };
            foreach (var item in arry)
                foreach (int element in item)
                    Console.WriteLine(element);
        }    
    }

数组抽象基类System.Array

System.Array是具体数组的抽象类,具体数组继承自System.Array类,通过Array可以创建一维,多维数组,并遍历数组。请看如下的程序示例:

        static void Main(string[] args)
        {
            //使用System.Array静态方法CreateInstance创建数组实例
            Array strArray = Array.CreateInstance(typeof(string), 4);
            strArray.SetValue("beijing", 0);
            strArray.SetValue("shanghai", 1);
            strArray.SetValue("tianjin", 2);
            strArray.SetValue("chongqin", 3);
            //上面的写法等价于
            Array strArray1 = new string[4]{"beijing","shanghai","tianjin","chongqin"};
            //foreach 遍历数组 
            foreach (var item in strArray)
                Console.WriteLine("foreach一维数组遍历list:{0}",item);
            //for 遍历数组
            for (int i = 0; i < strArray.Length; i++)
                Console.WriteLine("for一维数组遍历list:{0}",strArray.GetValue(i));
            //反转一维数组strArray
            Array.Reverse(strArray);
            //Array 创建多维数组
            Array strArray2 = Array.CreateInstance(typeof(int), new int[] { 3, 2 });
            strArray2.SetValue(10,new int[]{0,0});
            strArray2.SetValue(11, new int[] { 0, 1 });
            strArray2.SetValue(12, new int[] { 1, 0 });
            strArray2.SetValue(13, new int[] { 1, 1 });
            strArray2.SetValue(14, new int[] { 2, 0 });
            strArray2.SetValue(15, new int[] { 2, 1 });
            //上面的多维数组定义等价于
            int[,] strArray3 = new int[3,2] { { 10, 11 }, { 12, 13 }, { 14, 15 } };
            foreach (var item in strArray2)
                Console.WriteLine("foreach多维数组遍历:{0}",item);
           //0维度的长度为3,其上限为2,其下限为0,运行结果显示OK,OK
            if (strArray2.GetLength(0) == strArray2.GetUpperBound(0) - strArray2.GetLowerBound(0) + 1)
                Console.WriteLine("OK,OK");
        }
原创作品,转载时请注明作者及出处
原文地址:https://www.cnblogs.com/qqxz/p/5206817.html