I can not understand how advanced-For-Loop works [duplicate]

  • A+
Category:Languages

This question already has an answer here:

I learned that Advanced_For_Loop can take exactly two expressions. I don't know how it does work when it has three. I mean: for(expression1 : expression2 : expression3). And I don't know how it iterates in this code...

   private Sheet fieldVsTime(Workbook wb, String _title,List<PivotField>    _pivots, List<PivotField> _all,boolean _periods,Map<String, String>     _yAxesMapping) {      for (PivotField pivot : _all == null ? _pivots : _all) {         List<PivotField> list = pivot.getPivot();         if (list != null) {             for (PivotField pivotField : list) {                 int publishDate = Integer.parseInt(pivotField.getValue().toString());                 if (_periods) {                     publishDate = (publishDate / 10) * 10;                 }                 if (publishDate < minYear) {                     minYear = publishDate;                 }                 if (publishDate > maxYear) {                     maxYear = publishDate;                 }             }         }     } 

 


It only has two expressions:

for (PivotField pivot : (_all == null ? _pivots : _all))      ----------------   -------------------------------             1                       2 

The second expression (_all == null ? _pivots : _all) returns either _pivots or _all, depending on whether or not _all == null.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: