问题
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.
回答1:
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)
回答2:
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");
}
回答3:
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.
回答4:
Common Lisp supports both types of arrays.
The multidimensional array is called Array, while the "one-dimensional" one is called Vector.
来源:https://stackoverflow.com/questions/8177984/what-is-the-definition-of-a-true-multidimensional-array-and-what-languages-sup