Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:07:37+00:00 2026-06-12T21:07:37+00:00

In Java, why doesn’t the following line of code work? List<List<String>> myList = new

  • 0

In Java, why doesn’t the following line of code work?

List<List<String>> myList = new ArrayList<ArrayList<String>>();

It works if I change it to

List<ArrayList<String>> myList = new ArrayList<ArrayList<String>>();

At first, I thought maybe you can’t have lists of an interface, but I can create a List<Runnable> just fine.

Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:07:39+00:00Added an answer on June 12, 2026 at 9:07 pm

    Generic types are more pedantic.

    List means List or any sub-type, but <List> means only List. If you want a sub-type you need to have <? extends List>

    I suspect you can use

    List<List<String>> myList = new ArrayList<List<String>>();
    

    The reason you can’t do this is that you can be using a reference to a reference and with an extra level of indirection you have to be careful.

    // with one level of indirection its simple.
    ArrayList alist = new ArrayList();
    List list = aList; // all good
    list = new LinkedList(); // alist is still good.
    

    With generics you can have two level of indirection which can give you problems so they are more pedantic to avoid these issues.

    // with two levels of indirection
    List<ArrayList> alist = new ArrayList<ArrayList>();
    List<List> list = (List) alist; // gives you a warning.
    list.add(new LinkedList()); // adding a LinkedList into a list of ArrayList!!
    System.out.println(alist.get(0)); // runtime error
    

    prints

    Exception in thread "main" java.lang.ClassCastException: java.util.LinkedList
         cannot be cast to java.util.ArrayList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

But it doesn't work -meaning the java code is not executed. Although the batch
Alright, so Java doesn't allow the following: Foo<?> hello = new Foo<?>(); This makes
How come java doesn't allow the following generic return type: public <T extends Enum<T>
I'm new to Java and I have come to having the following problem: I
Can you please explain why it is possible to do: import java.util.ArrayList; import java.util.List;
Obviously, Java doesn't support unsigned number types natively, and that's not going to change
I've written the following, but Java doesn't like it! int limit = Math.round(Math.sqrt(inputNumber)); Suggestions
Possible Duplicate: Why does + work with Strings in Java? The following statements are
I know that java doesn't support multiple inheritance. So how can i fulfill following
Java doesn't support multiple inheritance for abstract classes, but I'm trying to do the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.