static void Main(string[] args)
{
int[] a = new int[] { 1, 3, 4, 8,30,10 };
int[] b = new int[] { 2, 5, 6, 9, 22,7,11};
//合并数组
int[] temp = new int[a.Length + b.Length];
a.CopyTo(temp, 0);
b.CopyTo(temp, a.Length);
#region 方案1 采用冒泡法
int tNum = 0;//中转值
string aa = string.Empty;
for (int i = 0; i < temp.Length; i++)
{
for (int j = i + 1; j < temp.Length ; j++)
{
if (temp[j] < temp[i])
{
tNum = temp[i];
temp[i] = temp[j];
temp[j] = tNum;
}
}
aa += temp[i].ToString() + " ";
}
Console.WriteLine(aa);
#endregion
#region 方案2 使用linq
string bb = string.Empty;
var strLinq = from s in temp orderby s select s;//升序排列
//var strLinq = from s in temp orderby s descending select s;//降序排列
foreach (var m in strLinq)
{
bb += m.ToString() + " ";
}
Console.Write(bb);
#endregion
}
C# 合并两个int数组 合并后根据大小排序 并输出
最新推荐文章于 2023-11-29 15:34:47 发布