What is the definition of a “true” multidimensional array and what languages ​​support them?

Most programming books I've ever read have the following line:

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

Since most of my experience is related to C languages, that is, to C ++, Java, JavaScript, php, etc., I'm not sure 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.

+6
source share
4 answers

C # supports both true multidimensional arrays, and arrays of "jagged" arrays (arrays of arrays), which can be a replacement.

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

Mesh arrays are generally considered the best because they can do everything a multidimensional array can do and more. In an uneven array, you can have every single array, but you cannot do this in a multidimensional array. There is even a code analysis rule for this ( http://msdn.microsoft.com/en-us/library/ms182277.aspx )

+6
source

Java uses them too

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

Here is an interesting use of it that I 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"); } 
0
source

Without missing the literals on Sun and Microsoft, this is what I remember from my days C. I hope this helps.

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

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

Saying this, x is a true two-dimensional array: after creating it, 300 locations (which can contain int) will be created, and you can use the well-known subscript convention to access this rectangular (with 15 rows and 20 columns) array, where you can go to x [row, col] by calculating (20 * row) + col.

However, in case y, when the structure is defined, only 15 pointers are allocated, but not initialized. (Initialization must be done explicitly)

There are advantages and disadvantages of this approach (an array of pointers or an array of arrays) or a jagged array, as it is called):

Advantage:

The rows of this array can have different lengths, that is, each y element should not point to twenty ROW elements; one element can point to 2 elements, the second element can point to 3 elements and from third to zero elements, etc.

Disadvantages:

However, in the best case scenario, if each element of y points to a twenty-element array, then 300 integer locations will be reserved, plus ten cells for additional pointers.

From the point of view of the current example, examples of C sharp examples given above (in one of the previous posts) should be given.

0
source

General Lisp supports both types of arrays.

A multidimensional array is called Array, and a “one-dimensional” array is called Vector.

0
source

Source: https://habr.com/ru/post/901784/


All Articles