Identifying last loop when using for each

后端 未结 25 1390
眼角桃花
眼角桃花 2020-12-08 09:59

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.

          


        
相关标签:
25条回答
  • 2020-12-08 10:01

    Foreach is elegant in that it has no concern for the number of items in a list and treats each element equally, I think your only solution will be using a for loop that either stops at itemcount-1 and then you present your last item outside of the loop or a conditional within the loop that handles that specific condition, i.e. if (i==itemcount) { ... } else { ... }

    0 讨论(0)
  • 2020-12-08 10:04

    What you are trying to do seems just a little too advanced for the foreach-loop. However, you can use Iterators explicitly. For example, in Java, I would write this:

    Collection<String> ss = Arrays.asList("A","B","C");
    Iterator<String> it = ss.iterator();
    while (it.hasNext()) {
        String s = it.next();
        if(it.hasNext())
            System.out.println("Looping: " + s);
        else 
            System.out.println("Last one: " + s);
    }
    
    0 讨论(0)
  • 2020-12-08 10:04

    <hello> what are we thinking here?

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = readIndex();
        String comp[] = str.split("}");
    
        StringBuffer sb = new StringBuffer();
        for (String s : comp) {
            sb.append(s);
            sb.append("}\n");
        }
        System.out.println (sb.toString());
    }
    

    As a modeling notation, the influence of the OMT notation dominates (e. g., using rectangles for classes and objects). Though the Booch "cloud" notation was dropped, the Booch capability to specify lower-level design detail was embraced. The use case notation from Objectory and the component notation from Booch were integrated with the rest of the notation, but the semantic integration was relatively weak in UML 1.1, and was not really fixed until the UML 2.0 major revision.

    0 讨论(0)
  • 2020-12-08 10:06

    How about this one? just learnt a little Ruby. hehehe

    list.collect {|x|(x!=list.last ? "Looping:"+x:"Lastone:"+x) }.each{|i|puts i}      
    
    0 讨论(0)
  • 2020-12-08 10:08

    Is this elegant enough? It assumes a non-empty list.

      list[0,list.length-1].each{|i|
        puts "Looping:"+i # if not last loop iteration
      }
      puts "Last one:" + list[list.length-1]
    
    0 讨论(0)
  • 2020-12-08 10:10

    Use join whenever possible.

    Most often the delimiter is the same between all elements, just join them together with the corresponding function in your language.

    Ruby example,

    puts array.join(", ")
    

    This should cover 99% of all cases, and if not split the array into head and tail.

    Ruby example,

    *head, tail = array
    head.each { |each| put "looping: " << each }
    puts "last element: " << tail 
    
    0 讨论(0)
提交回复
热议问题