中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#中動態(tài)調(diào)整數(shù)組大小的代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

通常,我們創(chuàng)建一個數(shù)組后就不能調(diào)整其長度,但是Array類提供了一個靜態(tài)方法CreateInstance用來創(chuàng)建一個動態(tài)數(shù)組,所以我們可以通過它來動態(tài)調(diào)整數(shù)組的長度。

namespace ArrayManipulation
{
    Class Program
    {
        static void Main (String[] args)
        {
            int[] arr = new int[]{1,2,3};
            PrintArr(arr);
  
            arr = (int[])Redim(arr,5);
            PrintArr (arr);
  
            arr = (int[]) Redim (arr, 2);
            PrintArr (arr);
  
        )
  
        public static Array Redim (Array origArray, int desiredSize)
        {
            //determine the type of element
            Type t = origArray.GetType().GetElementType();
  
            //create a number of elements with a new array of expectations
            //new array type must match the type of the original array
            Array newArray = Array.CreateInstance (t, desiredSize);
  
            //copy the original elements of the array to the new array
            Array.Copy (origArray, 0, newArray, 0, Math.Min (origArray.Length, desiredSize));
  
            //return new array
            return newArray;
        }
  
        //print array
        public static void PrintArr (int[] arr)
        {
            foreach (int x in arr)
            {
                Console.Write (x + ",");
            }
            Console.WriteLine ();
        }
    }
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:php代碼的加密解密

下一篇:C#對字符串進行壓縮和解壓縮的算法代碼