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.

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Can you explain the refactoring? I edited out the 'var', but that was just notation.
OK, I misread the 'at runtime'. That would mean @pete has a better answer.
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..

1 Comment

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.
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?

2 Comments

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

1 Comment

Huh, that I didn't know. That doesn't surprise me though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.