I have the following snippet
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
int N = rooms.size();
LinkedList<Integer> adj[] = new LinkedList[N];
int i=0;
for(List keys: rooms){
for(Integer key: keys){
adj[i].add(key);
adj[key].add(i);
}
i++;
}
return true;
}
}
I want to iterate using for-each only as I am trying to bring it into practice. I am getting error at the nested for loop saying object cannot be cast into Integer. I am unable to understand why it shouldnt work given rooms is a list of list of Integers. Why does it takes the datatype as Object for elements of keys.