Skip to content
Advertisement

Incompatible types List of List and ArrayList of ArrayList

The below line gives me error :

Incompatible Types.

List<List<Integer>> output = new ArrayList<ArrayList<Integer>>();

What is the reason?

EDIT

I understand if I change my second ArrayList to List, it does not give me error. I want to know the reason of error though. Thanks

Advertisement

Answer

If you had a List<List<Integer>> then you’d be able to add a LinkedList<Integer> to it. But you can’t do this for an ArrayList<ArrayList<Integer>>, so the latter can’t possibly be a type of List<List<Integer>>.

Advertisement