title: Copying the first half of an ArrayList
body: <p>There is an <code>ArrayList&lt;Integer&gt; al</code>, and I want to copy the first half of its elements into another <code>ArrayList&lt;Integer&gt; firstHalf</code>. (If al has an odd number of elements, <code>firstHalf</code> should have one more element.) However, the following code throws an <code>IndexOutOfBoundsException</code>, and says <code>Index: 0, Size: 0</code>, though I'm not sure how that would be a problem, since the indices of ArrayLists start at 0. Also, I am aware of <code>.arraycopy</code>, but I would like to do it this way, with for-loops.</p>
  
  <pre><code> int x = al.size()/2 + (al.size()%2) - 1;
   for(int i = 0; i &lt; x; i++){
      firstHalf.set(i, al.get(i));
   }
  </code></pre>