title: Sudoku solver with Exception in thread "main" java.util.ConcurrentModificationException on its Iterator
body: <p>I'm getting the following errors:</p>
  
  <pre><code>     Exception in thread "main" java.util.ConcurrentModificationException 
          at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
          at java.util.HashMap$KeyIterator.next(HashMap.java:845)
          at sudoku.Main.solve2(Main.java:143)
          at sudoku.Main.next2(Main.java:168)
          at sudoku.Main.solve2(Main.java:153)
          at sudoku.Main.main(Main.java:284) 
  </code></pre>
  
  <p>I don't understand the <code>java.util.HashMap$KeyIterator.next</code> and <code>java.util.HashMap$HashIterator.nextEntry</code> error messages, as I'm not able to get the keySet for a <code>HashSet</code> explicitly I assumed the Iterator was going through the keySet by default. </p>
  
  <p>I'm not using threads, just recursive calls.
  What's going on here? </p>
  
  <pre><code> static void solve2(int row, int col, int [][]grid,  ArrayList&lt;HashSet&lt;Integer&gt;&gt; availableNumsInRows,
            ArrayList&lt;HashSet&lt;Integer&gt;&gt; availableNumsInColumns){
  
       if (row&gt;=grid.length){
  
             System.out.println("solution found");
              printSolvedGrid(grid);
  
              System.out.println("move count for this sudoku is " + moveCounter);
              moveCounter=0; //reset counter
             return;
  
  
         }
  
         if( grid[row][col] != 0 ){
              next2( row, col, grid, availableNumsInRows, availableNumsInColumns ) ;
         }
  
         else {
           // Find a valid number for the empty cell
  
           Iterator &lt;Integer&gt; iterator = availableNumsInRows.get(row).iterator();
  
  
           for( int num = iterator.next() ; iterator.hasNext(); num = iterator.next())
           {
              if( checkRow(row,num,grid) &amp;&amp; checkCol(col,num,grid) &amp;&amp; checkBox(row,col,num,grid) )
              {
                 grid[row][col] = num ;
                 availableNumsInRows.get(row).remove(new Integer(num));
                 availableNumsInColumns.get(col).remove(new Integer(num));
                 moveCounter++;
  
                 //printSolvedGrid(grid);
                 next2( row, col, grid, availableNumsInRows, availableNumsInColumns );
  
              }
           }
  
           grid[row][col] = 0 ;
         }
  
    }
  
    //helper function for the first solution approach
    public static void next2( int row, int col, int [][] grid ,  ArrayList&lt;HashSet&lt;Integer&gt;&gt; availableNumsInRows,
            ArrayList&lt;HashSet&lt;Integer&gt;&gt; availableNumsInColumns )
     {
        if( col &lt; 8 ) //pass to next col
           solve2( row, col + 1, grid, availableNumsInRows, availableNumsInColumns) ;
        else //pass to next row
           solve2( row + 1, 0, grid, availableNumsInRows, availableNumsInColumns) ;
     }
  </code></pre>
  
  <p>Edit: </p>
  
  <p>I changed the code to:</p>
  
  <pre><code>   while (iterator.hasNext())
               {
  
                  num=iterator.next();
  
                  if( checkRow(row,num,grid) &amp;&amp; checkCol(col,num,grid) &amp;&amp; checkBox(row,col,num,grid) )
                  {
                     grid[row][col] = num ;
  
                     iterator.remove();
  
                     moveCounter++;
  
  
                     next2( row, col, grid, availableNumsInRows, availableNumsInColumns );
  
                  }
  
               }
  </code></pre>
  
  <p>and I'm still getting the <code>ConcurrentModificationException</code>, why is this? </p>
  
  <pre><code>Exception in thread "main" java.util.ConcurrentModificationException
          at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
          at java.util.HashMap$KeyIterator.next(HashMap.java:845)
          at sudoku.Main.solve2(Main.java:148)
          at sudoku.Main.next2(Main.java:175)
          at sudoku.Main.solve2(Main.java:137)
          at sudoku.Main.next2(Main.java:175)
          at sudoku.Main.solve2(Main.java:159)
          at sudoku.Main.next2(Main.java:175)
          at sudoku.Main.solve2(Main.java:137)
          at sudoku.Main.next2(Main.java:175)
          at sudoku.Main.solve2(Main.java:159)
          at sudoku.Main.next2(Main.java:175)
          at sudoku.Main.solve2(Main.java:159)
          at sudoku.Main.main(Main.java:291)
  Java Result: 1
  </code></pre>