【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();
        }

猜你喜欢

转载自blog.csdn.net/yf391005/article/details/83620674
今日推荐