title: Paginating an ArrayList
body: <p>I am trying to create a simple pagination routine for values held in an <code>ArrayList</code>. Basically what I want to do is render the first five elements in the <code>ArrayList</code> at first go. And then when users click on <code>Next (increment by another 5)</code> and <code>Previous (decrease by 5)</code>.</p>
  
  <p>My logic looks like this:</p>
  
  <pre><code>class foo
  {
     private static final int defaultStep = 5;
     private int moveCounter;
     private List&lt;String&gt; values;
  
     public foo()
     {
       values = new ArrayList&lt;String&gt;();
       values.add("Fiber Channel");
       values.add("Copper Channel");
       ...
     }
  
    private void pageNext()
    {
     if (moveCounter &gt; -1 &amp;&amp; moveCounter &lt; values.size())
     {
      int currentIndex = (moveCounter + 1);
      renderValues(currentIndex, false);
     }
    }
  
    private void pagePrevious()
    {
     if (moveCounter &gt; -1 &amp;&amp; moveCounter &lt;= values.size())
     {
      renderValues(moveCounter-1, true);
     }
    }
  
   private void renderValues(int startIndex, boolean isPreviousCall)
   {
    if (startIndex &gt; -1)
    {
     StringBuilder html = new StringBuilder();
     List&lt;String&gt; valuesToRender = new ArrayList&lt;String&gt;();
     int checkSteps = 1;
     while (startIndex &lt; values.size())
     {
       valuesToRender.add(values.get(startIndex));
       if (checkSteps == defaultStep) break;
       startIndex++;
       checkSteps++;
     }
     moveCounter = startIndex;
  
     //TODO: Build html String
     ...
     }
   }
  }
  </code></pre>
  
  <p>I have an issue with <code>pagePrevious</code> call, can you guys help me build the <code>valuesToRender</code> <strong>5-steps</strong> up <code>values array</code> before adding the value to render to the <code>valuesToRender</code> array.</p>
  
  <p>I tried doing something like this also:</p>
  
  <pre><code>  for (int start = startIndex, end = values.size() - 1; start &lt; end; start++, end--)
    {
      if (isPreviousCall) valuesToRender.add(values.get(end));
      else valuesToRender.add(values.get(start));
  
      if (checkSteps == defaultStep) break;
      checkSteps++;
    }
  </code></pre>
  
  <p>But this doesn't seems to work neither. Can you guys spot and help me fix this issue.
  Thanks Guys.</p>