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

  • SEARCH
  • Home
  • 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 5981867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:00:18+00:00 2026-05-22T22:00:18+00:00

How can I initialize a multidimensional List statically? This works: List<List<Integer>> list = new

  • 0

How can I initialize a multidimensional List statically?

This works:

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

But I’d like to init the list with some static lists like: (1,2,3), (4,5,6) and (7,8,9)

  • 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-05-22T22:00:19+00:00Added an answer on May 22, 2026 at 10:00 pm

    This is an old answer, but things have changed a bit. For java 9+ this can be done using the List.of() method which returns an immutable List which is a subclass of AbstractImmutableList.

    import java.util.List;
    
    List<List<Integer>> list = List.of(
                                   List.of(1, 2, 3),
                                   List.of(4, 5, 6),
                                   List.of(7, 8, 9)
                               );
    

    For older version of java or if one needs a mutable List the old answer still works:

    2011 answer

    If you create a helper method, the code looks a bit nicer. For example

    public class Collections {
        public static <T> List<T> asList(T ... items) {
            List<T> list = new ArrayList<T>();
            for (T item : items) {
                list.add(item);
            }
    
            return list;
        }
    }
    

    and then you can do (with a static import)

    List<List<Integer>> list = asList(
                                 asList(1,2,3),
                                 asList(4,5,6),
                                 asList(7,8,9),
                               );
    

    Why I don’t use Arrays.asList()

    Arrays.asList() returns a class of type java.util.Arrays.ArrayList (it’s an inner class of Arrays). The problem I’ve found is that it’s VERY easy to think that one is using a java.lang.ArrayList, but their implementation are very, very different.

    The comment above is also old, for java 9+ use List.of(E...elem).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can initialize an array like this: int [ ] arr = { 1,
I can initialize float32x4_t like this: const float32x4x4_t zero = { 0.0f, 0.0f, 0.0f,
In C++ you can initialize a variable in an if statement, like so: if
How can I initialize a const / static array of structs as clearly as
How can I initialize a list containing generic objects whose types can be different?
Can I really and truly trust .NET to initialize fields (like ints, structs and
In spring you can initialize a bean by having the applicationContext.xml invoke a constructor,
When {0} is used to initialize an object, what does it mean? I can't
How can I use a std::valarray to store/manipulate a 2D array? I'd like to
This question builds off of a previously asked question: Pass by reference multidimensional array

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.