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?
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.
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.
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.
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.
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.
(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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With