使用为每个时识别最后一个循环 [英] Identifying last loop when using for each

查看:132
本文介绍了使用为每个时识别最后一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的事与上次循环迭代不同的对象上执行的foreach时。我使用Ruby,但同样也适用于C#,Java的等。

I want to do something different with the last loop iteration when performing 'foreach' on an object. I'm using Ruby but the same goes for C#, Java etc.

  list = ['A','B','C']
  list.each{|i|
    puts "Looping: "+i # if not last loop iteration
    puts "Last one: "+i # if last loop iteration
  }

期望的输出是相当于

The output desired is equivalent to:

  Looping: 'A'
  Looping: 'B'
  Last one: 'C'

明显的解决方法是使用到code迁移到一个for循环为我1..list.length,但每个解决方案的感觉更优美。什么是最优雅的方式,以code循环过程中的特殊情况?它可以用foreach做什么?

The obvious workaround is to migrate the code to a for loop using 'for i in 1..list.length', but the for each solution feels more graceful. What is the most graceful way to code a special case during a loop? Can it be done with foreach?

推荐答案

如何获得参考最后一个项目,然后再使用它在foreach里面比较循环? 我不是说由KlauseMeier 提到你应该这样做,因为我自己会使用基于索引的循环。可惜我不知道红宝石所以下面的示例是在C#!希望你不介意: - )

How about obtaining a reference to the last item first and then use it for comparison inside the foreach loop? I am not say that you should do this as I myself would use the index based loop as mentioned by KlauseMeier. And sorry I don't know Ruby so the following sample is in C#! Hope u dont mind :-)

string lastItem = list[list.Count - 1];
foreach (string item in list) {
    if (item != lastItem)
    	Console.WriteLine("Looping: " + item);
    else	Console.Writeline("Lastone: " + item);
}

我修改了以下code。通过引用未值(只能使用引用类型不是值类型)进行比较。以下code应该支持包含相同的字符串(但不相同的字符串对象),因为MattChurcy的例子并没有指定字符串必须是不同的多个对象,我用LINQ,而不是计算指数Last方法。

I revised the following code to compare by reference not value (can only use reference types not value types). the following code should support multiple objects containing same string (but not same string object) since MattChurcy's example did not specify that the strings must be distinct and I used LINQ Last method instead of calculating the index.

string lastItem = list.Last();
foreach (string item in list) {
    if (!object.ReferenceEquals(item, lastItem))
        Console.WriteLine("Looping: " + item);
    else        Console.WriteLine("Lastone: " + item);
}

以上code的局限。 (1),它可以为字符串或引用类型不是值类型才能正常工作。 (2)相同的对象只能在列表中出现一次。可以具有包含相同的内容不同的对象。文字字符串不能重复使用,因为C#不创建具有相同内容的字符串,唯一的对象。

Limitations of the above code. (1) It can only work for strings or reference types not value types. (2) Same object can only appear once in the list. You can have different objects containing the same content. Literal strings cannot be used repeatedly since C# does not create a unique object for strings that have the same content.

和我没有愚蠢的。我知道基于环的索引是使用之一。我曾经这样说过,当我第一次张贴的初步答案。我的问题在背景提供最好的答案我可以。我太累了,保持这个解释可以让你一切只投中删除我的答案。我会,如果这一个消失太高兴了。谢谢

And i no stupid. I know an index based loop is the one to use. I already said so when i first posted the initial answer. I provided the best answer I can in the context of the question. I am too tired to keep explaining this so can you all just vote to delete my answer. I'll be so happy if this one goes away. thanks

这篇关于使用为每个时识别最后一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆