-1

I have come across a phenomenon I haven't encountered before while debugging our code.

Basically it seems like Java is skipping the last element of a collection during a for each loop.

The loop looks something like this:

 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;
     }
 }

Now the collection "rows" consists of several elements each inheriting the parent class "ReportRow" (Data1Row, GrossAmountRow, TotalAmountRow, FinalRow).

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.

My google search hasn't got me any satisfying results so I figured I would open a thread here.

Did anyone encounter something similar before? Or has an idea what could cause this problem? I am really at a loss here.

Some help would be much appreaciated!

Thank you in advance.

  • What kind of collection do you have? If it's for example a HashSet and the equals/hashcode implementation of your objects is wrong, it can also be a reason why the desired object is not in the collection. – u6f6o Nov 25 '15 at 12:14
  • 2
    So add an else statement and see if that runs. – Stultuske Nov 25 '15 at 12:14
  • I am using an ArrayList here and I already tried out an else statement, but it did nothing. I also just now noticed, that the size of the ArrayList is only 27, but it contains 28 elements, which would explain why the for loop would stop after the 27th element. Though it does not explain, why the size doesn't match the collection of elements in the list. – Tim Dieckhoff Nov 25 '15 at 12:23
  • Is your second last element an instance of `FinalRow`? – ShahiM Nov 25 '15 at 12:36

2 Answers2

4
 if (contentRow instanceof FinalRow) {
     return null;
 }

If you have two FinalRow or something else after a FinalRow Object, it will be "skipped" because the method stops after the first encounter.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • The list/collection only contains a single FinalRow Object. But I just saw that the size(27) of the list doesn't match the amount of elements (28). So I will have to see what might cause this first. – Tim Dieckhoff Nov 25 '15 at 12:28
0

I have solved the issue.

The "problem" was that I didn't notice that a subList was being used here and itÄs size was smaller than the original list (by only 1).

It only dawned on me after I already have commented on some of the answers in this thread.

Thank you at everyone who has tried to help!

  • can you please edit the question. as the solution you got and the question you asked does not seem to be in sync. – srv_sud Apr 19 '16 at 16:26