【问题标题】:Merging two lists which are already sorted in c#合并两个已经在c#中排序的列表
【发布时间】:2016-06-13 00:59:57
【问题描述】:

所以我有两个带有可交付成果的排序列表,我想合并它们。我以前从来没有做过这个操作,我不能真正让它工作。你可以在下面看到我写的方法。我无法弄清楚 while 语句,因为它一直触发异常。我不知道该怎么办...

public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
    int index1 = 0;
    int index2 = 0;

    while (a.Count() >= index1 || b.Count() >= index2)
    {
        if (a[index1].ID> b[index2].ID)
        {
            FinalDeliverables.Add(a[index1]);
            index1++;
        }
        else if (a[index1].ID < b[index2].ID)
        {
            FinalDeliverables.Add(a[index2]);
            index2++;
        }
        else if (a[index1].ID == b[index2].ID)
        {
            FinalDeliverables.Add(a[index1]);
            FinalDeliverables.Add(a[index2]);
            index1++;
            index2++;
        }
    }
}

【问题讨论】:

  • 看看MergeOrdered方法here

标签: c# merge


【解决方案1】:

我相信您得到的异常来自空指针。例如,如果您已经到达列表之一的末尾,但您的 while 循环仍在尝试比较值,则会发生这种情况。一种解决方法是在if 语句之前简单地添加一个检查,以查看是否已到达列表的一端(或两端)。如果是这样,则只需添加另一个列表中的其余项目。

public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
    int index1 = 0;
    int index2 = 0;

    while (true)
    {
        // if the end of the 'a' list has been reached, then add
        // everything from the 'b' list and break from the loop
        if (index1 >= a.Count()) {
            for (int i=index2; i < b.Count(); ++i) {
                FinalDeliverables.Add(b[i]);
            }
            break;
        }

        // if the end of the 'b' list has been reached, then add
        // everything from the 'a' list and break from the loop
        if (index2 >= b.Count()) {
            for (int i=index1; i < a.Count(); ++i) {
                FinalDeliverables.Add(a[i]);
            }
            break;
        }
        if (a[index1].ID > b[index2].ID)
        {
            FinalDeliverables.Add(a[index1]);
            index1++;
        }
        else if (a[index1].ID < b[index2].ID)
        {
            FinalDeliverables.Add(a[index2]);
            index2++;
        }
        else if (a[index1].ID == b[index2].ID)
        {
            FinalDeliverables.Add(a[index1]);
            FinalDeliverables.Add(a[index2]);

            index1++;
            index2++;
        }
    }
}

请注意,此重构的副作用是您原来的 while 循环不再需要检查边界。相反,当列表之一用完时,循环将终止。

另请注意,此解决方案假定您的输入列表按降序顺序排序。

【讨论】:

  • 在 for 循环中,索引 1 和 2 出现错误:仅赋值调用递增递减
  • 是的,这解决了问题。但我在 FinalDeliverables.Add(a[index1]);有什么想法吗?
  • 这个异常发生在哪一行?您用于a 和b 的样本数据是什么?
  • 最后一个 else if 语句的第一行
  • 这不应该发生 AFAIK。如果您可以调试您的代码并让我知道a 和b 的状态,那么也许我可以进一步帮助您。如果您可以访问a[index1].ID,那么我认为您没有理由不能将a[index1] 添加到列表中。
【解决方案2】:

您可以为此目的使用 LINQ 扩展方法,MergeLists 的方法签名将如下所示:

public void MergeLists(List<Deliverable> a, List<Deliverable> b) 
{
    var finalList = a.Concat(b);
    List<Deliverable> FinalSortedList = finalList.OrderBy(x => x.ID).ToList();
}

或者你可以像下面这样修改你自己的方法:在此之前让我假设以下,输入列表a中的元素计数将始终大于b的计数。所以你需要做的是在调用方法之前检查元素的数量。所以调用将如下所示:

if(a.Count>b.Count)
   MergeLists(a,b);
else
   MergeLists(b,a);

您提到两个输入是排序的,让我假设这些列表按升序排序。现在考虑以下代码:

public void MergeLists(List<Deliverable> a, List<Deliverable> b)
{
    int largeArrayCount = a.Count;
    int currentBIndex = 0;
    List<Deliverable> FinalResult = new List<Deliverable>();
    for (int i = 0; i < largeArrayCount; i++)
    {
        if (i < b.Count)
        {
            if (a[i].ID >= b[i].ID)
            {
                // Add All elements of B Which is smaller than current element of A
                while (a[i].ID <= b[currentBIndex].ID)
                {
                    FinalResult.Add(b[currentBIndex++]);
                }
            }
            else
            {
                FinalResult.Add(a[i]);
            }
        }
        else
        {
            // No more elements in b so no need for checking
            FinalResult.Add(a[i]);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多