Java collection nested List of list

Source: Internet
Author: User

This return value is encountered on Leetcode

 Public class Solution {    public list<list<integer>> levelorder (TreeNode root) {            }} 

List<list<integer>>

That is, the list content is the type of list, the direct use of list<list<integer>> list = new list<list<integer>> () is wrong, because list is the interface, Cannot instantiate (cannot instantiate the type list<list<integer>>).

But if you use

1 list<list<integer>> List = new linkedlist<linkedlist<integer>> ();

An error will be added (cannot convert from linkedlist<linkedlist<integer>> to list<list<integer>>),

The correct approach is to modify it into:

1 New Linkedlist<linkedlist<integer>>(); 3 or 5 New Linkedlist<list<integer>> ();

This is possible, that is, the generic type parameters must be the same.

The following treatment

1 New Arraylist<arraylist<string>>(); 2 or 3 New Linkedlist<linkedlist<string>> ();

It is also possible to do so without referencing the implementation class with the interface class .

Reference

1.Working with a List of Lists in Java

2.How do I initialize a two-dimensional List statically?

3.Initialize list<list<integer>> in Java

Java collection nested List of list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.