9

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.

GaGar1n
  • 1,040
  • 2
  • 10
  • 17

4 Answers4

11

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);
nawfal
  • 70,104
  • 56
  • 326
  • 368
H H
  • 263,252
  • 30
  • 330
  • 514
  • 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 Mar 16 '10 at 22:19
  • Can you explain the refactoring? I edited out the 'var', but that was just notation. – H H Mar 16 '10 at 22:22
  • OK, I misread the 'at runtime'. That would mean @pete has a better answer. – H H Mar 16 '10 at 22:24
1

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
  • 10,712
  • 4
  • 44
  • 91
  • 1
    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 Mar 16 '10 at 22:16
1

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
  • 11,313
  • 4
  • 43
  • 54
  • 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 Mar 16 '10 at 22:36
  • 1
    the for loop is incrementing 'rows, not 'i ... afaik: you will not be able to cast the created Array returned by this into any other form of Array. – BillW May 05 '19 at 02:43
-1

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
  • 19,423
  • 9
  • 68
  • 97