title: Java: for each loop is skipping last element of collection
body: <p>I have come across a phenomenon I haven't encountered before while debugging our code.</p>
  
  <p>Basically it seems like Java is skipping the last element of a collection during a for each loop.</p>
  
  <p>The loop looks something like this:</p>
  
  <pre><code> for (ReportRow contentRow : rows) {            
       if (contentRow instanceof Data1Row) {
           doSomething1();
       }
       if (contentRow instanceof GrossAmmountRow) {
           doSomething2();
       }
       if (contentRow instanceof TotalAmountRow) {
           doSomething3();
       }
       if (contentRow instanceof FinalRow) {
           return null;
       }
   }
  </code></pre>
  
  <p>Now the collection "rows" consists of several elements each inheriting the parent class "ReportRow" (Data1Row, GrossAmountRow, TotalAmountRow, FinalRow).</p>
  
  <p>The problem is now that the last element of the collection is getting skipped every time the loop is executed. I have already debugged the method several times to verify whether or not the collection still contains the element, but the element is present all the time - it's solely getting skipped.</p>
  
  <p>My google search hasn't got me any satisfying results so I figured I would open a thread here.</p>
  
  <p>Did anyone encounter something similar before? Or has an idea what could cause this problem? I am really at a loss here.</p>
  
  <p>Some help would be much appreaciated!</p>
  
  <p>Thank you in advance.</p>