【发布时间】:2013-12-22 08:43:42
【问题描述】:
我想知道是否有一种简单的方法来比较两个整数数组并在第一个数组源中按顺序添加缺失的元素。
示例代码:
//Two list [source , compare ]
public int[] ListSource = new int[]{ 4,8,12,20,24,32 } ;
public int[] ListToCompare = new int[] { 3, 8, 16, 16, 20, 24, 28, 32,36 };
//If size of array is different , we resize the source
if(ListSource.Length != ListToCompare.Length)
{
Array.Resize(ref ListSource, ListToCompare.Length);
}
// Compare and update
for(int a = 0 ; a < ListToCompare.Length;a++)
{
if(ListSource[a] != ListToCompare[a])
{
// .... How can i do it
}
}
//And finally my Listsource is complete merge with the listToCompare and in good order
//ListSource = { 3, 4, 8, 12, 16, 16, 20, 24, 28, 32 };
非常感谢:-)
【问题讨论】:
-
输入是否保证已经排序?
-
如果这些来源总是排序的(所以顺序是已知的),你可以
Union和OrderBy。如果不是 - 它将不适用。 -
不应该在您的示例答案末尾有
36吗? -
你的结果中的 16 是不是打错字了?
-
是的,很抱歉打错了“16”
标签: c# .net arrays merge compare