Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a true multi dimensional array?

I was reading a book about Javascript and saw this line;

JavaScript does not support true multidimensional arrays, but you can approximate them with arrays of arrays.

What's the difference?

like image 310
Sinan Avatar asked Dec 30 '11 23:12

Sinan


People also ask

What is a multi-dimensional array?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

What is an example of a multidimensional array?

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.

What is true about the multidimensional array in C language?

Single dimensional array only stores single data or information like marks of the student. But in some cases, you have to store complex data which have rows and columns. So, a single dimensional array can't have this type of feature.

What is one-dimensional and multi-dimensional array?

A one-dimensional array stores a single list of various elements having a similar data type. A two-dimensional array stores an array of various arrays, or a list of various lists, or an array of various one-dimensional arrays. Representation. It represents multiple data items in the form of a list.


2 Answers

A true multi-dimensional array must be indexed with multiple indices. An array of arrays can be indexed with a single index, which will return another array. A true multi-dimensional array stores all its data contiguously. An array of arrays stores all its constituent arrays arbitrarily scattered around. This can improve iteration performance because of cache effects for true arrays.

like image 128
recursive Avatar answered Sep 18 '22 19:09

recursive


(a visual explanation that complements an excellent answer of @recursive)

In some languages (C#) there are both. The difference is in "shape" of such arrays.

int[3, 4] // true two-dimensional array

// it will "look" like this, rectangular shape
[[0, 0, 0, 0]
 [0, 0, 0, 0]
 [0, 0, 0, 0]]

But when you define an array of arrays, it can easily (especially in javascript) look like this. It's called a jagged array.

[[0, 0]
 [0, 0, 0, 0, 0, 0]
 [0, 0, 0]]
like image 37
Sergio Tulentsev Avatar answered Sep 20 '22 19:09

Sergio Tulentsev