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 7613871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:15:33+00:00 2026-05-31T02:15:33+00:00

I am defining an array like so: int [][] intervals = new int[10][10]; BUT

  • 0

I am defining an array like so: int [][] intervals = new int[10][10]; BUT my array will have to have different dimensions, depending on who calls the function in which this array is defined.

I wanted to try and do something like this: int [][] intervals = new int[][]; but it says “Variable must provide either dimension expressions or an array initializer”

I also tried int [][] intervals = null, but afterwards when i try and do intervals[3][4] = 10 it gives exception;

So how can i accomplish this?

  • 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-31T02:15:35+00:00Added an answer on May 31, 2026 at 2:15 am

    Try to think of a multi-dimension array as an array of arrays

    It’s not exactly like that inside the JVM, but that’s the best mental model to work with for these sorts of questions.

    So, if you have

    int [][] intervals = null;
    

    then your outer-array is null, and can later be initialised with

    int size = 10;
    intervals = new int[size][];
    

    Which creates an array of int[], but in that case each of the 10 inner-arrays are null.

    If you want them all to be the same size (e.g. an 10 x 5 array) then do something like:

    int size1 = 10;
    int size2 = 5;
    intervals = new int[size1][size2];
    

    But if you want them to be different sizes then do something like:

    int size = 10;
    intervals = new int[size][];
    intervals[0] = new int[5];
    intervals[1] = new int[4];
    // etc...
    

    But all of these assume that you know how big you want your array to be before you start filling it.
    If you don’t know that, then you want to use a List instead – have a look at ArrayList.

    Edit
    I’ve just seen in your comment that this latter case is what you want, so …

    Try something like:

    List< List<Integer> > list = new ArrayList< List<Integer> >();
    
    
    for(int i=0; i<10; i++) // You would loop over whatever input you're processing
    {                       //  but I don't know what you're doing in that part of you code
                            //  so I'll just do it 10 times.
    
        List<Integer> innerList = new ArrayList< Integer >();
    
        innerList.add( 1 );
        innerList.add( 2 );
        innerList.add( 3 );
    
        // etc.
    
        list.add( innerList );
    }
    

    Alternatively, you could have:

    • List< int[] > which would mean using a List for the outside collection, but an array for the inner collection, if you know how long your inner collection is going to be.
    • List<Integer>[] which uses an array for the outer collection, and a list for the inner collection.

    It really depends on exactly how your code needs to work.

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

Sidebar

Related Questions

If I write int *columns[32]; am I defining an array with 32 pointers to
In C, when defining an array I can do the following: int arr[] =
I have the following code defining a Functor template and the function running_op, which
I need an array which I can access from different methods, I have to
I am defining a new block to clean up my view like so: class
I have a function that returns an array of variables. The variables it returns
When Defining a constructor in a class I initially had something like: public function
I have an array in php like this: $myArray = array('name'=>'juank', 'age'=>26, 'config'=>array('usertype'=>'admin','etc'=>'bla bla'));
I have an array like this arr = [orange,red,black,white] I want to augment the
Defining custom components in Facelets is easy and quick but there's one thing I

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.