将一个数组反转通过算法去实现虽然可以简化,但是如果实际项目中使用还有一种更快速的办法:简单,有效。
1 public class ReverseList<T> : IList<T>
2 {
3 private IList<T> _list;
4 public ReverseList(IList<T> list)
5 {
6 this._list = list;
7 }
8
9 public T this[int index]
10 {
11 get
12 {
13 return _list[this.Count - 1 - index];
14 }
15 }
16
17 }
18
19
20 //test
21 DateTime t1 = DateTime.Now;
22 IList<int> list = new List<int>();
23 for (int i = 1; i <= 2048 * 2048; i++)
24 {
25 list.Add(i);
26 }
27 DateTime t2 = DateTime.Now;
28 list = new ReverseList<int>(list);
29 DateTime t3 = DateTime.Now;
30 Console.WriteLine("Create Array:{0}", ((TimeSpan)(t2 - t1)).TotalMilliseconds);
31 Console.WriteLine("Reverse Array:{0}", ((TimeSpan)(t3 - t2)).TotalMilliseconds);
32 Console.WriteLine(list[0]);
33 Console.ReadKey();
2 {
3 private IList<T> _list;
4 public ReverseList(IList<T> list)
5 {
6 this._list = list;
7 }
8
9 public T this[int index]
10 {
11 get
12 {
13 return _list[this.Count - 1 - index];
14 }
15 }
16
17 }
18
19
20 //test
21 DateTime t1 = DateTime.Now;
22 IList<int> list = new List<int>();
23 for (int i = 1; i <= 2048 * 2048; i++)
24 {
25 list.Add(i);
26 }
27 DateTime t2 = DateTime.Now;
28 list = new ReverseList<int>(list);
29 DateTime t3 = DateTime.Now;
30 Console.WriteLine("Create Array:{0}", ((TimeSpan)(t2 - t1)).TotalMilliseconds);
31 Console.WriteLine("Reverse Array:{0}", ((TimeSpan)(t3 - t2)).TotalMilliseconds);
32 Console.WriteLine(list[0]);
33 Console.ReadKey();
详细的代码请在这里下载