Skip to content
Advertisement

Initialize List<List> in Java

How can I initialize List<List<Integer>> in Java?

I know List is an interface and I can use ArrayList or LinkedList to implement List<Integer> list = new ArrayList<Integer>(), but when I initialize List<List<Integer>> list = new ArrayList<ArrayList<Integer>>(); I get error incompatible types:

ArrayList<ArrayList<Integer>> cannot be converted to List<List<Integer>>.

So how can I proceed?

Advertisement

Answer

Use

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

or since Java 1.7

List<List<Integer>> list = new ArrayList<>();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement