数组--元素顺序反转

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 /*
 7  练习7:将一个字符串数组的元素的顺序进行反转。
 8  * {"3","a","8","haha"} 转换为{"haha","8","a","3"}。
 9  * 提示:第i个和第length-i-1个进行交换。
10  */
11 namespace ConsoleApplication1
12 {
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             //声明一个字符串数组,并赋值
18             string[] st = new string[] { "3", "a", "8", "haha" };
19             //string[] st = new string[] { "3", "a", "8", "haha" ,"qw"};
20             string s;//声明一个中间数值
21 
22             //对数组长度进行单双判断
23             if (st.Length % 2 == 0)
24             {
25                 for (int i = 0; i < st.Length / 2; i++)
26                 {
27                     s = st[i];
28                     st[i] = st[st.Length -i- 1];
29                     st[st.Length -i- 1] = s;
30                 } 
31             }
32             if (st.Length % 2 == 1)
33             {
34                 for (int i = 0; i < ((st.Length-1) / 2); i++)
35                 {
36                     s = st[i];
37                     st[i] = st[st.Length -i- 1];
38                     st[st.Length-i - 1] = s;
39                 }
40             }
41             foreach (var a in st)
42                 {
43                     Console.Write(" "+a);
44                 }
45 
46             Console.ReadKey();
47         }
48     }
49 }
原文地址:https://www.cnblogs.com/pengyouqiang88/p/5023365.html