简体   繁体   中英

Java defining multidimensional array

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?

When creating a multidimensional array in Java, you have to specify at least the dimension of the outermost array. In your case, if you want all the arrays to be of a different size, you will still need to specify how many arrays you need at all. For example, if you need six arrays, you could say

int [][] intervals = new int[6][];

You could then fill them in like this:

intervals[0] = new int[137];
intervals[1] = new int[42];
/* ... */

If you don't know in advance how many arrays you'll need, you might want to consider using an ArrayList<int[]> to explicitly add in new arrays. For example:

ArrayList<int[]> intervals = new ArrayList<int[]>();
intervals.add(new int[137]);
intervals.add(new int[42]);
/* ... */

Hope this helps!

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 (eg 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.

If you want to allocate elements in array dynamically, may be you should consider ArrayList :

ArrayList< ArrayList< int > > array = new ArrayList< ArrayList< int > >();
array.add( new ArrayList< int >() );
array.get( 0 ).add( 1 );
array.get( 0 ).add( 2 );
array.add( new ArrayList< int >() );
array.get( 1 ).add( 3 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM