void topologicalSortUtil(int v, boolean visited[], Stack<Integer> stack, ArrayList<Integer>[] adj) {
visited[v] = true;
for (int i : adj[v]) {
if (!visited[i]) {
topologicalSortUtil(i, visited, stack, adj);
}
}
stack.push(v);
}
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.
@Alex Shesterov and others, Here is the code using which adj was generated.
ArrayList[] arr = new ArrayList[n+1];
for(int i=0; i<=n; i++){
arr[i] = new ArrayList<Integer>();
}
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));
ArrayList<Integer>
type and in your foreach loopint i
cannot hold an arraylist – Pavneet_Singh Oct 31 '16 at 14:16Arrays
class. – RealSkeptic Oct 31 '16 at 14:22