title: java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer
body: <pre><code>void topologicalSortUtil(int v, boolean visited[], Stack&lt;Integer&gt; stack, ArrayList&lt;Integer&gt;[] adj) {
          visited[v] = true;
          for (int i : adj[v]) {
              if (!visited[i]) {
                  topologicalSortUtil(i, visited, stack, adj);
              }
          }
  
          stack.push(v);
      }
  </code></pre>
  
  <p>Here, I am calling an array of arraylist. The error is in the enhanced for loop, as told by my IDE (IntelliJ). It seems like a perfectly valid call as adj[v] would return an ArrayList and the integer i catching each elements of the array as they come and then processing them in the recursive call.</p>
  
  <p>@Alex Shesterov  and others, 
     Here is the code using which <i>adj</i> was generated.</p>
  
  <pre><code>    ArrayList[] arr = new ArrayList[n+1];
          for(int i=0; i&lt;=n; i++){
              arr[i] = new ArrayList&lt;Integer&gt;();
          }
          arr[1].add(Arrays.asList(8,6,2));
          arr[2].add(Arrays.asList(7));
          arr[3].add(Arrays.asList(6));
          arr[4].add(Arrays.asList(5,2));
          arr[5].add(Arrays.asList(4,2,3));
          arr[6].add(Arrays.asList(3,2,5));
          arr[7].add(Arrays.asList(2));
          arr[7].add(Arrays.asList(1,2,4));
  </code></pre>