【问题标题】:c# how to trim null elements at the start and end of a listc#如何在列表的开头和结尾修剪空元素
【发布时间】:2013-03-28 08:39:49
【问题描述】:

给定一个对象列表,其值为

StatusList = new List<StatusData>
                {
                    new StatusData {Count = 0, Id = 1},
                    new StatusData {Count = 0, Id = 2},
                    new StatusData {Count = 1, Id = 3},
                    new StatusData {Count = 0, Id = 4},
                    new StatusData {Count = 2, Id = 5},
                    new StatusData {Count = 3, Id = 6},
                    new StatusData {Count = 0, Id = 7},
                    new StatusData {Count = 0, Id = 8},
                    new StatusData {Count = 0, Id = 2}
                };

如何通过删除带有零的元素来修剪列表的左侧和右侧?

【问题讨论】:

  • 那里的第四个元素呢?也应该删除吗?
  • 第四个元素没有被删除。如果是这样,它只需要删除所有计数为零的元素。

标签: c# list for-loop


【解决方案1】:
int start = 0, end = StatusList.Count - 1;

while (start < end && StatusList[start].Count == 0) start++;
while (end >= start && StatusList[end].Count == 0) end--;

return StatusList.Skip(start).Take(end - start + 1);

【讨论】:

    【解决方案2】:

    使用 LINQ:

    var nonEmptyItems = StatusList.Where(sd => sd.Count > 0);
    

    nonEmptyItems 将包含 Count 大于 0 的项目,包括中间的项目。

    或者,如果您不希望删除该中心项,则可以使用 while 循环并从前面和后面删除每个空项,直到不存在此类项为止。

    var trimmed = false;
    while(!trimmed)
    {
      if(StatusList[0].Count == 0)
         StatusList.RemoveAt(0);
    
      if(StatusList[StatusList.Count - 1].Count == 0)
        StatusList.RemoveAt(StatusList.Count - 1);
    
      if(StatusList[0].Count == 0 > StatusList[StatusList.Count - 1].Count > 0)
        trimmed = true;
    }
    

    【讨论】:

    • @RoeeGavirel - 是的。我说过。
    • @RoeeGavirel - 我发现很多时候人们并没有准确地发布他们所追求的内容。
    【解决方案3】:
    // Remove until count != 0 is found
    foreach (var item in StatusList.ToArray())
    {
        if (item.Count == 0)
            StatusList.Remove(item);
        else
            break;
    }
    // Reverse the list
    StatusList.Reverse(0, StatusList.Count);
    // Remove until count != 0 is found
    foreach (var item in StatusList.ToArray())
    {
        if (item.Count == 0)
            StatusList.Remove(item);
        else
            break;
    }
    // reverse back
    StatusList.Reverse(0, StatusList.Count);
    

    【讨论】:

      【解决方案4】:

      效率不高但更具可读性的解决方案是:

      StatusList.Reverse();
      StatusList = StatusList.SkipWhile(x => x.Count == 0).ToList();
      StatusList.Reverse();
      StatusList = StatusList.SkipWhile(x => x.Count == 0).ToList();
      

      【讨论】:

        【解决方案5】:

        我就是这样解决的...

        RemoveOutsideZeros(ref StatusList);
                        StatusList.Reverse();
        RemoveOutsideZeros(ref StatusList);
        
        
        private void RemoveOutsideZeros(ref List<StatusData> StatusList)
        {
            bool Found0 = false;
            foreach (StatusData i in StatusList.ToList())
            {
                if (i.Count != 0)
            {
                Found0 = true;
            }
                if (i.Count == 0 && Found0 == false)
                {
                    StatusList.Remove(i);
                }
            }
        

        }

        【讨论】:

        • 您的解决方案将为空列表和仅包含 Count = 0 项的列表抛出异常
        猜你喜欢
        • 1970-01-01
        • 2011-03-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2021-05-12
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多