string[] s = new string[3] { "我", "是", "好人" }; for (int i = 0; i < s.Length/2; i++) //若字符串个数为奇数,则交换了(s.length-1)/2,若是偶数,则交换了s.length/2. { string temp = s[i]; s[i] = s[s.Length - 1-i]; //定义一个新的变量实现交换。 s[s.Length - 1-i] = temp; for (int j = 0; j < s.Length; j++) { Console.Write(s[j]); //输出交换完的数组。。 } } Console.ReadKey();
例2:
string[] arry = new string[] { "张三", "李四", "赵五", "孙六", "周⑦", "王八" }; int count = arry.Length; for (int i = 0; i < count / 2; i++) { string temp = arry[i]; arry[i] = arry[count - 1 - i]; arry[count - 1 - i] = temp; } for (int j = 0; j < count; j++) { Console.Write(arry[j] + " "); } Console.ReadLine();