Not able to understand how to define a List of List in java -


i want understand difference between 2 definitions , why correct 1 correct , wrong wrong.

the 1 showing me compile-error

list<list<integer>> arr2 = new arraylist<arraylist<integer>>();   

the error gave me :

 try2.java:8: error: incompatible types: arraylist<arraylist<integer>> cannot converted list<list<integer>>                 list<list<integer>> arr2 = new arraylist<arraylist<integer>>(); 

the 1 working:

list<arraylist<integer>> arr = new arraylist<arraylist<integer>>(); 

note:

i understand why below 1 works:

list<integer> arr = new arraylist<integer>();   

edit-1:

now want understand wrong list<list<integer>> arr2 = new arraylist<arraylist<integer>>();

you use diamond operator, ghostcat suggested, , let compiler worry correct type.

but if want understand correct type should be, use:

list<list<integer>> arr2 = new arraylist<list<integer>>();  

you instantiating list of (let's forget second happens list<integer>), need create instance of class implements list interface - arraylist in case. create instance of arraylist of something.

the element type (my called "something") - list<integer> in example - remains unchanged.

now, when want add element list, need create instance of class implements list<integer>:

list<integer> inner = new arraylist<integer>(); arr2.add(inner); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -