将一个字符串数组的元素的顺序进行翻转。。
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] + "\t"); } Console.ReadLine();
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步