1. 程式人生 > >【C#基礎】將一個字串陣列的元素進行反轉

【C#基礎】將一個字串陣列的元素進行反轉

 

Array.Reverse(str);//反轉整個陣列中元素的順序

實現原理 :

        static void Main(string[] args)
        {            
            string[] str = {"a","u","c","d","e","b" };                                      
            for (int i = 0; i < str.Length/2; i++)
            {
                //無論陣列元素個數是奇數還是偶數,都交換Length/2次
                string temp = str[i];
                str[i] = str[str.Length - 1 - i];//交換變數的值
                str[str.Length - 1 - i] = temp;
            }
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine(str[i]);
            }
            Console.ReadKey();
        }