【问题标题】:ForEach, get value of next propertyForEach,获取下一个属性的值
【发布时间】:2013-11-23 17:12:42
【问题描述】:

我正在使用ForEach statement 将属性值分配给一个类。输出将如下所示...

Startingpoint | length
-----------------------
0             | 1
1             | 54.47
55.47         | 47.98 

所以长度是这样计算的……

length = *next item starting point - current item starting point

这就是我想以伪代码式的外观做的事情。

foreach (DefectMap obj in dm)
{
     mdmList.Add(new ModifiedDefectMap()
        {                        
            StartingPoint = obj.Start,
            Length =  ValueOfNext().obj.Start - obj.Start

问题是,我无法知道"ValueOfNext().obj.Start" 会是什么,因为我正在foreach 循环的当前迭代中。

【问题讨论】:

  • 1. option - 使用 for 循环 2. option - 使用 indexof
  • 我已经稍微修正了格式。希望你不介意

标签: c# loops foreach iteration


【解决方案1】:

使用常规的 for 循环

//Count-1 to stop at the second to last item
for(int i = 0; i < dm.Count-1; ++i)
{
      mdmList.Add(new ModifiedDefectMap()
      {                        
           StartingPoint = dm[i].Start,
           Length =  dm[i+1].Start - dm[i].Start
      });
}

【讨论】:

  • 这可行,但我希望它遍历最后一个循环,然后将 Length 设置为 0,我将努力解决这个问题
【解决方案2】:

你可以试试.Zip() 声明。

mdmList = dm.Zip(dm.Skip(1), (current, next) => new ModifiedDefectMap
{
  StartingPoint = current.Start,
  Length = next.Start - current.Start
});

skip 会偏移列表,因此您最终会将所有相邻元素对映射到currentnext,然后使用它们创建对象。这样看起来还算干净。

【讨论】:

  • 我喜欢这个 zip 声明,我以前从未使用过它。使用 zip 是否有一种简单的方法来表示 if next == null then Length = 0
  • 是的,它本质上是一个匿名函数,所以你可以在那里做任何你想做的事情。您还可以将整个(当前,下一个)部分作为以下形式的方法:private ModifiedDefectMap CreateModifiedMap(DefectMap current, DefectMap next),然后 zip 将变为 mdmList = dm.Zip(dm.Skip(1), CreateModifiedMap);
【解决方案3】:

如果您想一次访问多个项目,最好使用for 循环。

for(int i = 0; i < dm.Count-1; ++i)
{
      mdmList.Add(new ModifiedDefectMap()
      {                        
           StartingPoint = dm[i].Start,
           Length =  dm[i+1].Start - dm[i].Start
      }
}

如果您仍想使用foreach 循环,您可以使用IndexOf

【讨论】:

    【解决方案4】:

    我不确定您是否可以查看 foreach 中的下一个值,但是,如果您正在执行类似的实现,我建议您使用 for 循环来提供下一个值,请务必检查如果可能的下一个值在数组的范围内。

    for(int i = 0; i < list.Length; i++)
    {
         list[i] = random.Next();
         if(i+1 < list.Length)
         {
             list[i+1] = random.Next();
         }
    }
    

    请务必了解您需要 list 具有索引器([x] 数组表示法),因为某些集合没有索引器,例如 HashSet 或 Stack。

    【讨论】:

      【解决方案5】:

      有几个选项建议自己。

      1. 使集合 (dm) 成为一个链表,而不是任何它。 然后你可以写

         while(obj.next != null)
         {
            var length =  obj.Next.Start - obj.Start;
            // whatever else you need to do in iteration
         }
        
      2. 为每个对象添加一个名为 Collection 的属性,其中包含对集合 dm 的引用,以及另一个名为 End 的属性。

         public double? End // has to be nullable double to return null on last item
         { 
           get 
           { 
               var nDx = Collection.IndexOf(this);
               return Collection.Count > nDx+1?  Collection[nDx + 1].Start: (double?)null;
           }
         }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多