unity游戏开发-C#语言基础篇(Array数组相关)

 class Program
    {
        static void Main(string[] args)
        {
            //-------------------一维数组------------------
           // int a = 1;//声明一个整型变量;
            int[] Arr1 = new int[5];//定义一维数组长度;
            int[] arr1 = new int[3] { 1, 2, 3 };//定义一维数组长度并初始化赋值;实例化;
            int[] arr_1 = { 1, 2, 3 };//省略长度;直接给一维数组赋值;

            //--------------------多维数组----------------
            int[,] Arr2 = new int[,] { };//多维数组的定义;
            int[,] Arr_2 = new int[2, 3];//多维数组的行,列;
            int[,] arr_22 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
            int[,] arr2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//多维数组直接赋值;

            //--------------------交错数组------------------------

            int[][] Arr3 = new int[2][];//必须要定义列;否则报错;
            int[][] arr3 = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };//定义初始化并赋值;列数可以改变;多维数组不可改变;
            int[][] arr_3 = { new int[] { }, new int[] { }, new int[] { } };//定义交错数组,注意交错数组里的一维数组:new int[]{};


            //------------Array对象使用方法----------------

            int[] arrA = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] arrB = { 18, 28, 38, 48, 58, 68, 78, 88, 98 };
            int[,] arrC = { { 1, 2, 3 }, { 4, 5, 6 } };
            int[][] arrD = { new int[] { 1, 2, 3, 4 }, new int[] { 6, 7, 8 }, new int[] { 9, 0 } };
            Console.WriteLine(arrD.Rank);//数组的维度;
            Console.WriteLine(arrD.Length);//数组长度;
            Console.WriteLine(arrD.LongLength);//数组元素的总和;

            Console.WriteLine(arrD.IsFixedSize);//获取一个值;表示是否有固定的值;

            Console.WriteLine(arrD.IsReadOnly);//判断数组是否可读;


            Show.show1(arrA, "前: ");
            Array.Clear(arrA, 2, 3);//数组紧跟的是下标;从数组下标开始将长度个数赋值为零;参数1:数组;参数2:下标;参数3:元素个数为零;
            Show.show1(arrA, "后: ");


            bool[] arrTF = { true, true, true, true, false, false, false };

            Show.showBool(arrTF, "前: ");
            Array.Clear(arrTF, 1, 2);//布尔类型
            Show.showBool(arrTF, "后: ");


            string[] arrStr = { "Hello", " ", "world", "!" };

            Show.showStr(arrStr, "前");
            Array.Clear(arrStr, 1, 2);//string 类型
            Show.showStr(arrStr, "后");

            int[] arrE = { 1, 1, 1 };
            int[] arrF = { 8, 8, 8, 8, 8, 8, 8, 8, 8 };

            Show.show1(arrF, "前: ");
            Array.Copy(arrE, arrF, arrE.Length);//把数组E中的长度个数元素复制到数组F头部中;参数1:被复制的数组,参数2:要复制的数组;参数3:注意是复制元素的长度个数(个数),可以不必要全部复制;
            Show.show1(arrF, "后: ");

            int[] arrE1 = { 1, 1, 1 };
            int[] arrF1 = { 8, 8, 8, 8, 8, 8, 8, 8, 8 };
            Show.show1(arrF1, "前: ");
            arrE1.CopyTo(arrF1, arrF1.Length - arrE1.Length);//把整个数组E1从指定下标复制到数组F1中去;参数1:被复制的数组;参数2:要复制的数组;参数3:操作下标;注意是从下标开始;
            Show.show1(arrF1, "后: ");



            int[] arrAA = { 1, 2, 3, 4, 5, 6, 7, 8 };
            int[] arrBB = { 21, 22, 23, 24, 25, 26, 27, 28 };
            arrAA.SetValue(88, 1);//设置数组下标的值;参数1:数组;参数2:要设置的值;参数3:设置值的下标;数组.SetValue(设值,下标);
            Console.WriteLine("获取下标的是:" + arrAA.GetValue(1));//获取数组下标的值;参数1:数组;参数2:下标; 数组.GetValue(下标);

            Show.show1(arrBB, "前: ");
            Array.Reverse(arrBB);//数组逆转 参数1:要逆转的数组;
            Show.show1(arrBB, "后: ");

            //---------------------练习-----------------
            //int[] arrE={33,36,66,23}; 转变为36,66,33,23

            int[] arrEE = { 33, 36, 66, 23 };
            int[] arrEE1 = new int[arrEE.Length];

            Show.show1(arrEE, "前: ");

            arrEE.CopyTo(arrEE1, 0);
            Array.Copy(arrEE, 1, arrEE1, 0, 2);
            Array.Copy(arrEE, 0, arrEE1, 2, 1);
            Show.show1(arrEE1, "后: ");


            Console.ReadKey();




        }
    }
class Show
    {
        public static void show1(int[] arr, string str)
        {
            Console.Write(str);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }


        public static void showBool(bool[] arr, string str)
        {
            Console.Write(str);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();

        }



        public static void showStr(string[] arr, string str)
        {
            Console.Write(str);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }

    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(Array数组相关))