title: iterating loops comparing the current index with the next index java
body: <p>Here is my code</p>
  
  <pre class="lang-java prettyprint-override"><code>String endDate;
  int currentIndex = 0;
  int nextIndex = currentIndex + 1;
  while(currentIndex &lt; aList.size() - 1)
  {
      endDate = aList.get(nextIndex).getStartDate();
      if(endDate == null)
      {
          endDate = episodeEnd.toString();
      }
      aList.get(currentIndex).setEndDate(endDate);
      currentIndex++;
      nextIndex++
  }
  </code></pre>
  
  <p>aList looks like this:</p>
  
  <pre><code>[itemA, 2019-09-18, 2019-11-13],
  [itemB, 2019-11-13, 2019-12-19],
  [itemC, 2019-12-19, null]
  </code></pre>
  
  <p>itemC end date will always be null since there is no itemD.</p>
  
  <p>The point of the code is to make the endDate of each item the same as the start date of the next item. If there is no next item then the end date should be the end date of a separate time period defined elsewhere in the code I.E. episodeEnd</p>
  
  <p>The problem I'm having is iterating the list properly.</p>
  
  <p>The loop never reaches the end as I need to set the last index to aList.size() - 1 to prevent indexOutOfRange. Since it never reaches the end, itemC never gets checked therefore, itemC end date remains null.</p>
  
  <p>My other thought was to use nested for loops with the outer loop looking at the currentIndex and the nested loop looking at the </p>
  
  <p>How do I code this so that itemC gets checked and avoid an indexOutOfRange?</p>