Trying to move elements of a list form one to another, until one is empty; ConcurrentModificationException
<p><strong>NB:</strong> Please see my edit below:</p>

<p>I've been trying to write a card shuffling simulation java program for a while now. The high level overview is this:</p>

<ul>
<li>Make a deck</li>
<li>Cut <code>myDeck</code> into my <code>handLeft</code> and <code>handRight</code></li>
<li>While my <code>handRight</code> is not empty:
<ul>
<li>Move a random number of cards from the right hand to the left.</li>
</ul></li>
</ul>

<p>This moving part is done by the method below:</p>

<pre><code>public ArrayList&lt;Card&gt; leftShuffle(int fudge){
    Random randomGenerator = new Random();
    int numberOfCardsToMove = 0;
    while(!handRight.isEmpty()){
        numberOfCardsToMove = randomGenerator.nextInt(fudge);
        if (numberOfCardsToMove&lt;handRight.size()){
            List&lt;Card&gt; cardsBeingMoved = handRight.subList(0, numberOfCardsToMove);
            handRight.removeAll(cardsBeingMoved);  &lt;---line 81
            handLeft.addAll(cardsBeingMoved);      &lt;---line 82
        }else{
            handLeft.addAll(handRight);
            handRight.clear();
        }
    }
    return handLeft;
}
</code></pre>

<p>However, when I try to add cards to the SubList <code>cardsBeingMoved</code> java freaks out and gives me this exception:</p>

<pre><code>Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1091)
at java.util.ArrayList$SubList.size(ArrayList.java:921)
at java.util.AbstractCollection.toArray(AbstractCollection.java:136)
at java.util.ArrayList.addAll(ArrayList.java:497)
at org.Craig.CardShuffling.Scientist.leftShuffle(Scientist.java:82)
at org.Craig.CardShuffling.Scientist.shuffle(Scientist.java:49)
at org.Craig.CardShuffling.Launcher.main(Launcher.java:11)
</code></pre>

<p>Now I'm familiar with iterators and how they work, but I don't think the standard <code>while(it.hasNext)</code> loop will work here. I've seen one attempt at a solution <a href="http://stackoverflow.com/a/8585533/1075247">here</a> but I'm not sure if it maps neatly on to my problem. Removing something is not causing this issue, adding something unrelated to the while clause is causing the issue. Otherwise I could change the condition to something like <code>while(handLeft.size!=52)</code> and then remove from <code>handRight</code> at the end.</p>

<p>I've tried using the CopyOnWriteArrayList class, but that errors the same way (see appendix A), I've tired various 'holding' lists so that the same list isn't being altered at the same time but it still give the exception.</p>

<p>How do I get past this issue? Is there a clever way to use iterators I've not thought of yet?</p>

<hr>

<h3>Appendix A</h3>

<p><strong>Shuffle Method</strong></p>

<pre><code>    public ArrayList&lt;Card&gt; shuffle (int fudge, Shuffling method){
    cutDeck(fudge);
    switch(method){
    case right:
        myDeck.setDeck(rightShuffle(fudge));
        break;
    case left:
        myDeck.setDeck(leftShuffle(fudge));
        break;
    case both:
        myDeck.setDeck(bothShuffle(fudge));
        break;
    }
    return myDeck.getDeck();
}
</code></pre>

<p>
<strong>CopyOnWriteArrayList exception stack trace.</strong></p>

<pre><code>Exception in thread "main" java.util.ConcurrentModificationException
at java.util.concurrent.CopyOnWriteArrayList$COWSubList.checkForComodification(CopyOnWriteArrayList.java:1130)
at java.util.concurrent.CopyOnWriteArrayList$COWSubList.size(CopyOnWriteArrayList.java:1170)
at java.util.AbstractCollection.toArray(AbstractCollection.java:136)
at java.util.concurrent.CopyOnWriteArrayList.addAll(CopyOnWriteArrayList.java:770)
at org.Craig.CardShuffling.Scientist.leftShuffle(Scientist.java:82)
at org.Craig.CardShuffling.Scientist.shuffle(Scientist.java:49)
at org.Craig.CardShuffling.Launcher.main(Launcher.java:11)
</code></pre>

<h3>Appendix B:</h3>

<p>I've changes the method to this:</p>

<pre><code>    public ArrayList&lt;Card&gt; leftShuffle(int fudge){
    Random randomGenerator = new Random();
    int numberOfCardsToMove = 0;
    while(!handRight.isEmpty()){
        numberOfCardsToMove = randomGenerator.nextInt(fudge);
        if (numberOfCardsToMove&lt;handRight.size()){
            List&lt;Card&gt; cardsBeingMoved = handRight.subList(0, numberOfCardsToMove);
            handRight.removeAll(cardsBeingMoved);
            /* cardBeingMoved is just a view, 
             * clearing it removes the same cards from handRight*/
            cardsBeingMoved.clear();
        }else{
            handLeft.addAll(handRight);
            handRight.clear();
        }
        System.out.println("Size of Deck one:" + handRight.size() + "\n");
        System.out.println("Size of Deck one:" + handLeft.size() + "\n");
    }
    return handLeft;
}
</code></pre>

<p>Now the exception is this.</p>

<pre><code>Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1091)
at java.util.ArrayList$SubList.size(ArrayList.java:921)
at java.util.AbstractList.clear(AbstractList.java:234)
at org.Craig.CardShuffling.Scientist.leftShuffle(Scientist.java:86)
at org.Craig.CardShuffling.Scientist.shuffle(Scientist.java:49)
at org.Craig.CardShuffling.Launcher.main(Launcher.java:11)
</code></pre>
