tags:

views:

75

answers:

4

In my project I have a lot of code like this:

int[][] a = new int[firstDimension][];
for (int i=0; i<firstDimension; i++)
{
  a[i] = new int[secondDimension];
}

Types of elements are different.

Is there any way of writing a method like

createArray(typeof(int), firstDimension, secondDimension);

and getting new int[firstDimension][secondDimension]?

Once again, type of elements is known only at runtime.

+1  A: 

Well you can do this:

int[,] array = new int[4,2];

What you get is called a multidimensional array (4x2). Here is a nice article about multidimensional arrays.

The term jagged array usually refers to arrays, that have different second dimensions. For example take:

int[][] jagged = new int[2][];
jagged[0] = new int[5]; // 5 elements
jagged[1] = new int[1]; // 1 element

so this is not a 2x5 array, but a jagged array..

m0sa
That would be too easy :)But I need to use the second dimensions as an arrays, for example: int[] a = array[3], which I can not do with multidimensional array.
GaGar1n
+3  A: 

Generics should do the trick:

    static T[][] CreateArray<T>(int rows, int cols)
    {
        T[][] array = new T[rows][];
        for (int i = 0; i < array.GetLength(0); i++)
            array[i] = new T[cols];

        return array;
    }

You do have to specify the type when calling this:

  char[][] data = CreateArray<char>(10, 20);
Henk Holterman
Thanks, it's a big help for me, but that would require a huge refactoring in an existing project.After initializing, arrays are used only as jagged arrays.
GaGar1n
Can you explain the refactoring? I edited out the 'var', but that was just notation.
Henk Holterman
OK, I misread the 'at runtime'. That would mean @pete has a better answer.
Henk Holterman
Sorry, my bad. Your solution fits well, thanks.
GaGar1n
A: 

As mOsa mentioned, if you want rectangular jagged array, then you are better off using a multi-dimensional array.

int[,] array = new int[dimension, dimension2];

will generate a rectangular array.

The reason to use a jagged array, is if you want to create an array with different secondary dimensions. A jagged array is really an array of arrays, where as a multi-dimensional array is a slightly different beast.

Alastair Pitts
A jagged array is faster.
Henk Holterman
Huh, that I didn't know. That doesn't surprise me though.
Alastair Pitts
+1  A: 

If:

  • You definitely want a jagged array, and not a multi-dimensional one as mOsa mentions.
  • You definitely need it to be of dynamic type at runtime, and not at compile time using generics as Henk mentions.

You can use Array.CreateInstance something like:

static Array CreateArray (Type t, int rows, int cols)
{

    Array arr = Array.CreateInstance (typeof(Array), rows);
    for (int i = 0; i < rows; rows++) {
        arr.SetValue (Array.CreateInstance(t, cols), i);
    }

    return arr;
}

But are you sure you need this to by dynamic type at runtime?

Pete
Thanks, tried this too. By the way, there is an error - should be 'Array arr = Array.CreateInstance (Array, rows)' I think :)The problem was that i couldn't refer to elements like arr[i][k] then. Casting to type[][] didn't work too.Actually, you're right, I can forgot about dynamic types and use generics.
GaGar1n