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?

3
  • 2
    Are you asking for an array that changes size dynamically at runtime? That is a List<Integer>. To simulate a two-dimensional array use List<List<Integer>> i.e. a list containing lists of Integer. Commented Mar 11, 2012 at 6:33
  • 1
    The first thing you tried seems to be right. maybe you mixed that array indexes start with 0 ? Commented Mar 11, 2012 at 6:33
  • Uh yeah...something like that...i don't know the dimension of the array from the beginning, so i don't want to define a huge array just for 2 elements. Commented Mar 11, 2012 at 6:34

3 Answers 3

4

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Great...i know the number of the columns (it's 3 every time), but in this case i'll just consider it on it's side...transposed or whatever it's called.. Thanks i think this will help ;)
@AndreiBogdan If you know how big the outside collection is (columns in your case) then you might want to use ArrayList<Integer>[] instead. That makes it an array of lists (where templatetypedef's solution is a list of arrays).
3

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.

Comments

1

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 );

1 Comment

aham... but i really need to use int[][] :) Otherwise I would have used array lists :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.