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

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

问题描述

在对象上执行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
  }

所需的输出相当于:

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

显而易见的解决方法是使用 'for i in 1..list.length' 将代码迁移到 for 循环,但 for each 解决方案感觉更优雅.在循环期间对特殊情况进行编码的最优雅方式是什么?可以用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 提到的基于索引的循环.抱歉,我不懂 Ruby,所以下面的示例是用 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);
}

我修改了下面的代码,通过引用而不是值进行比较(只能使用引用类型而不是值类型).以下代码应该支持包含相同字符串(但不是相同字符串对象)的多个对象,因为 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);
}

上述代码的局限性.(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

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

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