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

4

11

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.

Healy answered 18/11, 2011 at 4:58 Comment(2)
As far as i have heard Fortran-90 used to have itOrchestral
@Shekhar_Pro: every FORTRAN version I've used (back thru 66) has, up to 7 dimensions.Suspense
O
8

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)

Olshausen answered 18/11, 2011 at 5:16 Comment(4)
Thanks for the answer. I understand what a true multidimensional array is now. However, keep in mind that in C and C++ sub-arrays cannot be of different size even though they use arrays of arrays. Also, is C# the only language that supports them?Healy
" C++ sub-arrays cannot be of different size even though they use arrays of arrays.". No they don't use arrays of arrays.Holy
@Holy " C++ sub-arrays cannot be of different size even though they use arrays of arrays." Why this isn't true? multi-dimensional array in C++ is array of arrays.Landsman
"Array of arrays" means that the 1st array contains a set of other arrays (i.e. distinct objects), which is not the case in C/C++. int[5][2] is a single object. Something like int*[5] could be considered as an array of arrays.Holy
A
0

Java uses them too

int[][] a2 = new int[10][5];

Here's an interesting use of it that I've found

String[][] Data;

 //Assign the values, do it either dynamically or statically
 //For first fow
 Data[0][0] = "S"; //lastname
 Data[0][1] = "Pradeep"; //firstname
 Data[0][2] = "Kolkata"; //location

 //Second row
 Data[1][0] = "Bhimani"; //lastname
  Data[1][1] = "Shabbir"; //firstname
  Data[1][2] = "Kolkata"; //location

 //Add as many rows you want

 //printing
 System.out.print("Lastname\tFirstname\tLocation\n");
 for(i=0;i<2;i++)
 {
   for(j=0;j<3;j++)
   {
     System.out.print(Data[i][j]+"\t");
   }
   //move to new line
   System.out.print("\n");
 }
Augustaugusta answered 18/11, 2011 at 5:15 Comment(0)
K
0

Without going through the reams of literature on the Sun and Microsoft sites, this is what I remember from my C days. Hope this helps.

To make it simple, if we just think in 2 dimensions - Arrays can either be represented as a two-dimensional array and an array of pointers. In code this amounts to int x[15][20]; int *y[15];

In this example, x[5][6] and b[5][6] are both valid syntactically and end up referring to a single int.

That being said, x is a true two-dimensional array: Once you create it , there will be 300 locations (that can contain int) that have been set aside, and you can use the well known subscript convention to access this rectangular (with 15 rows and 20 columns) array where you can get to x[row,col] by calculating (20 * row) + col.

However in case of y, while the structure is being defined, only 15 pointers are allocated, but not initialized. (Initialization will need to be done explicitly)

There are advantages and disadvantages of this approach (pointer array or "array of arrays" or jagged array as it is called):

Advantage:

The rows of this array can be of different lengths i.e. each element of y does not need to point to a twenty-element ROW; one element may point to a 2 elements, 2nd element may point to 3 elements, and 3rd to zero elements and so on.

Disadvantage:

However given a best case scenario, if each element of y does point to a twenty-element array, then there will be 300 integer locations set aside, plus ten cells for the pointers which is additional.

From a current example perspective, the C sharp examples given above (in one of the previous posts) should suffice.

Kant answered 18/11, 2011 at 5:52 Comment(2)
In your example, x[0] would just point to the first array. Also, because arrays decay to pointers in C, x is not a true multidimensional array even though it would be contiguous in memory and you can use pointer arithmetic to access the arrays. This is why C has x[ ][ ] syntax but not x[ , ].Healy
@JesseGood: x is a true multi-dimensional array, C having array-decay notwithstanding.Reardon
P
0

Common Lisp supports both types of arrays.

The multidimensional array is called Array, while the "one-dimensional" one is called Vector.

Prevaricator answered 27/10, 2017 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.