title: Iterate over consecutive object pairs in an ArrayList
body: <p>I want to get pairs of objects from an ArrayList so I can perform calculations between the elements of each object. Ideally it should iterate over pairs of objects. For example in a List with {obj1, obj2, obj3, obj4} it should go over {obj1,obj2}, {obj2,obj3} and {obj3,obj4}. </p>
  
  <p>What I have tried so far is as follows.</p>
  
  <pre><code>public class Sum {
      public ArrayList&lt;Double&gt; calculateSum(ArrayList&lt;Iter&gt; iter) {
          ListIterator&lt;Iter&gt; it = iter.listIterator();
          ArrayList&lt;Double&gt; sums = new ArrayList&lt;&gt;();
  
          while (it.hasNext()) {
              Iter it1 = it.next();
              Iter it2;
              if(it.hasNext()){
                  it2 = it.next();
              } else {    break;  }
  
              double sum = it1.getValue() + it2.getValue();
              sums.add(sum);
          }
          return sums;
      }
  }
  </code></pre>
  
  <p>Here, it just iterates as {obj1,obj2} and {obj3,obj4}. How can I fix this?</p>
  
  <p>All help is greatly appreciated. Thanks! </p>