unity游戲開發-C#語言基礎篇(Array數組(一))

 class Program
    {
        static void Main(string[] args)
        {
            int[] arrA = new int[6] { 1, 2, 3, 4, 5, 6 };
            int[] arrB = { 4, 5, 6, 14, 15, 16, 18 };
            int[,] arr_1 = { { 1, 2, 3 }, { 4, 5, 6 } };//二維數組
            int[][] arr_2 = { new int[] { }, new int[] { }, new int[] { } };//交錯數組
            //arrA.Rank 數組的維度
            //arrA.LongLength 數組所有維度的元素個數;
            //arrA.IsFixedSize 表示里面有元素;

            Console.WriteLine(arrA.Rank);
            Console.WriteLine(arrA.IsFixedSize);
            Console.WriteLine(arrA.IsReadOnly);
            //arrA.IsReadOnly 判斷是否只讀,返回布爾值;

            ShowClass.ShowArr(arrA, "改變前");
            Array.Clear(arrA, 1, 4);//把指定位置改為零 參數1:數組 參數2:下標 參數3:長度個數

            ShowClass.ShowArr(arrA, "改變后");


            bool[] arrT = new bool[] { true, false, true, false, true, true, true };

            ShowClass.ShowArrbool(arrT, "\n改變前:");
            Array.Clear(arrT, 1, 3);

            ShowClass.ShowArrbool(arrT, "\n改變后:");


            Array.Copy(arrA, arrB, 3);//把數組A相應長度個數復制數組B頭部;參數1:被復制數組 參數2:要復制數組;參數3:要賦值數組的長度個數;


            ShowClass.ShowArr(arrB, "\n改變后");
            Array.Copy(arrB, arrA, 3);//練
            ShowClass.ShowArr(arrA, "\n改變后");

            ShowClass.ShowArr(arrB, "\n改變前");
            //int[] arrE = {11,22,33 };
            //arrE.CopyTo(arrB,2);//把一個數組賦值到另一個數組的相應下標位;注意數組長度;最大值:arrB-arrE;
            ShowClass.ShowArr(arrB, "改變前");

            //練
            int[] arr_A = { 1, 2, 3, 4, 5, 6 };
            int[] arr_B = { 8, 18, 28, 38, 48, 58, 68, 78, 88, 98, 108 };
            ShowClass.ShowArr(arr_B, "\n前");
            Array.Copy(arr_A, arr_B, 3);
            arr_A.CopyTo(arr_B, 3);
            ShowClass.ShowArr(arr_B, "\n后");

            Console.WriteLine(arr_B.GetValue(2));//獲取相應下標位置的值;

            Console.WriteLine(Array.IndexOf(arr_B, 18));//尋找指定元素在數組第一次出現的下標;沒有則返回-1;
            Array.IndexOf(arr_B, 108);
            Array.LastIndexOf(arr_B, 18);//尋找指定元素在數組最后一次出現的下標,沒有返回-1;

            Console.WriteLine(ShowClass.ArrIndexOf(arr_B, 18));

            ShowClass.ShowArr(arr_B, "\n前: ");
            Array.Reverse(arr_B);//數組逆轉
            arr_B.SetValue(888, 2);//設置數組相應下標位置的元素值 跟getValue向相對應
            ShowClass.ShowArr(arr_B, "\n后: ");


            //int[] arrE={33,36,66,23}; 轉變為36,66,33,23

            int[] arrE = { 33, 36, 66, 23 };
            int[] arr_E = new int[arrE.Length];
            ShowClass.ShowArr(arrE, "\n前:");
            arrE.CopyTo(arr_E, 0);
            Array.Copy(arrE, 1, arr_E, 0, 2);
            Array.Copy(arrE, 0, arr_E, 2, 1);
            ShowClass.ShowArr(arr_E, "\n后:");

            Console.ReadKey();      
        }
    }
 class ShowClass
    {
        /// <summary>
        /// 一維數組顯示 傳字符串對比
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="str"></param>
        public static void ShowArr(int[] arr, string str)
        {

            Console.Write(str);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }

        /// <summary>
        /// bool型顯示
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="str"></param>
        public static void ShowArrbool(bool[] arr, string str)
        {

            Console.Write(str);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }
        /// <summary>
        /// 二維數組顯示
        /// </summary>
        /// <param name="arr"></param>
        public static void ShowArrerwei(int[,] arr)
        {
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }

        }

        /// <summary>
        /// 尋找第一次出現的下標
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="a"></param>
        /// <returns></returns>
        public static int ArrIndexOf(int[] arr, int a)
        {
            int index = -1;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == a)
                {
                    index = i;
                    break;
                }


            }

            return index;

        }


        public static int ArrLastIndexOf(int[] arr, int a)
        {

            int index = -1;
            for (int i = arr.Length - 1; i > 0; i--)
            {
                if (arr[i] == a)
                {
                    index = i;
                    break;
                }
            }

            return index;
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容