Search code examples
language-agnosticmultidimensional-arrayjagged-arrayslanguage-theory

What is the definition of a "true" multidimensional array and what languages support them?


Most of the programming books I have ever read, have the following line:

"X language does not support true multidimensional arrays, but you can simulate (approximate) them with arrays of arrays."

Since most of my experience has been with C-based languages, i.e. C++, Java, JavaScript, php, etc., I'm not sure of what a "true" multidimensional array is.

What is the definition of a true multidimensional array and what languages support it? Also, please show an example of a true multidimensional array in code if possible.


Solution

  • C# supports both true multi-dimensional arrays, and "jagged" arrays (array of arrays) which can be a replacement.

    // jagged array
    string[][] jagged = new string[12][7];
    
    // multidimensional array
    string[,] multi = new string[12,7];
    

    Jagged arrays are generally considered better since they can do everything a multi-dimensional array can do and more. In a jagged array you can have each sub-array be a different size, whereas you cannot do that in a multi-dimensional array. There is even a Code Analysis rule to this effect (http://msdn.microsoft.com/en-us/library/ms182277.aspx)