5

I confuse between double[][] and double[,] in C#.
My teammate give me a function like this:

public double[][] Do_Something(double[][] A)
{
     .......
}

I want to use this function:

double[,] data = survey.GetSurveyData(); //Get data
double[,] inrma = Do_Something(data);

It lead an error: invalid argument.
I don't want to edit my teammate's code.
Does it have any way to convert double[][] to double [,] ?

Thanks!

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Ngo Van
  • 857
  • 1
  • 21
  • 37
  • 10
    `double[][]` is a jagged array. `double[,]` is a multidimensional array. They are definitely not the same type, and their uses differ rather a lot. Use the right type for the right task. – J. Steen Jul 24 '13 at 12:49
  • possible duplicate of [Why we have both jagged array and multidimentional array?](http://stackoverflow.com/questions/4648914/why-we-have-both-jagged-array-and-multidimentional-array) and [What is differences between Multidimensional array and Array of Arrays in C#?](http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays-in-c) – Cédric Bignon Jul 24 '13 at 12:52
  • Are you or your teammates sure about what is the difference between them? – Soner Gönül Jul 24 '13 at 12:52

4 Answers4

9

A double[][] is an array of double[] (An array of arrays) but double[,] is a single 2 dimensional double array

Example :

double[] Array1 = new double[] {1,2,3};
double[] Array2 = new double[] {4,5,6};
double[][] ArrayOfArrays = new double[][] {Array1,Array2};
double[,] MultidimensionalArray = new  double[,] {{1,2}, {3,4}, {5,6}, {7,8}};   
sm_
  • 2,572
  • 2
  • 17
  • 34
8

double[][] and double[,] have different meanings.

double[][] is jagged, so some elements can be of different lengths than others.

double[,] is "rectangular", so all elements are of the same length.

You could write a method to "convert" between the two, but how will you rectify the differences? I.e. how will you decide to "trim" from the long elements or expand the short elements in order to make it rectangular?

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
1
static T[,] Convert<T>(T[][] array)
{
    if (array == null)
        throw new ArgumentNullException("array");
    if (array.Length == 0)
        return new T[0, 0];
    T[,] retval = new T[array.Length, array[0].Length];
    for (int i = 0; i < array.Length; i++)
        for (int j = 0; j < array[i].Length; j++)
            if (array[i].Length != retval.GetLength(1))
                throw new Exception();
            else
                retval[i, j] = array[i][j];
    return retval;
}
Denis
  • 5,894
  • 3
  • 17
  • 23
  • 1
    That only works with a non-jagged jagged array, to work with all inputs, you can use `width = array.Max(a => a.Length)`, and fill the empty slots with `default(T)`. If you do want to have the check in there, you should move it out a level, so it's only checked once per row, rather than for every cell. – RoadieRich Jul 24 '13 at 13:05
1

They are two different types of arrays.

double[][] is a jagged array, which is an array of arrays; each of these arrays can have a different length, which leads to a problem.

double[,] is just a multidimensional array. Each row will have an equal number of columns and each column will have an equal number of rows.

This size difference causes a problem since the jagged array could really be different dimensions for different rows. You could write a method to convert between the two if you knew the exact dimensions of the jagged array, but in that case I would suggest rewriting the original method to accept and return a multidimensional array (double[,]).

Daniel Underwood
  • 2,191
  • 2
  • 22
  • 48