Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize List<List<Integer>> in Java

Tags:

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?

like image 604
Ram Swami Avatar asked May 22 '15 16:05

Ram Swami


People also ask

How do you create a list of integers and strings in Java?

You can do this as follows but have to give up on generics for the list container. List<List> listOfMixedTypes = new ArrayList<List>(); ArrayList<String> listOfStrings = new ArrayList<String>(); ArrayList<Integer> listOfIntegers = new ArrayList<Integer>(); listOfMixedTypes. add(listOfStrings); listOfMixedTypes.

What is list of () in Java?

The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.


2 Answers

Use

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

or since Java 1.7

List<List<Integer>> list = new ArrayList<>();
like image 95
Reimeus Avatar answered Nov 14 '22 10:11

Reimeus


You can define it as List<List<Integer>> list = new ArrayList<List<Integer>>();.

Then while defining the inner List you can take care of initialising it as ArrayList<Integer>.

like image 44
shruti1810 Avatar answered Nov 14 '22 10:11

shruti1810