c# 合并两个有序数组

 1 int[] ints1 = new int[] { 2, 5, 6, 10, 15, 19 };
 2             int[] ints2 = new int[] { 3, 7, 11, 18 };
 3             ArrayList lists = new ArrayList();
 4             ArrayList temp = new ArrayList();
 5             lists.AddRange(ints1);
 6             temp.AddRange(ints2);
 7             for (int i = 0; i < lists.Count; i++)
 8             {
 9                 if (temp.Count > 0)
10                 {
11                     var strs2First = temp[0];//取第一个元素
12                     if (Int32.Parse(lists[i].ToString()) >= Int32.Parse(strs2First.ToString()))
13                     {
14                         lists.Insert(i, strs2First);
15                         temp.RemoveAt(0);
16                     }
17                     else if (Int32.Parse(lists[lists.Count - 1].ToString()) < Int32.Parse(strs2First.ToString()))
18                     {
19                         lists.Insert(lists.Count, strs2First);
20                         temp.RemoveAt(0);
21                     }                    
22                 }                
23             }

 

posted @ 2017-02-17 10:37  Z&K  阅读(1363)  评论(0编辑  收藏  举报