Possible Duplicate:
What is differences between Multidimensional array and Array of Arrays in C#?
Can anybody explain me the difference between string[][]
and string[,]
?
Possible Duplicate:
What is differences between Multidimensional array and Array of Arrays in C#?
Can anybody explain me the difference between string[][]
and string[,]
?
string[,]
is a multidimensional array .
string[][]
is a Jagged array :
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
string[,]
- Multidimensional Arrary (Rectangular array)
Multidimensional can have more than one dimension. The following example shows how to create a two-dimensional array of two rows and two columns.
Declaration :
string[,] contacts;
Instantiation:
string[,] contacts = new string[2,2];
Initialization :
string[,] contacts = new string[2, 2] { {"John Doe","johndoe@example.com"}, {"Jane Doe","janedoe@example.com"} };
string[ ][ ]
- Jagged Array (Array-of-arrays)
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array-of-arrays."
A jagged array can store efficiently many rows of varying lengths. Any type of data, reference or value, can be used. Indexing jagged arrays is fast. Allocating them is somewhat slow.
Jagged arrays are faster than multidimensional arrays
Declaration :
string[][] contacts;
Instantiation:
string[][] contacts = new string[2][];
for (int i = 0; i < contacts.Length; i++)
{
contacts[i] = new string[3];
}
Initialization :
string[][] contacts = new string[2][] { new string[] {"john@example.com","johndoe@example.com"}, new string[] {"janedoe@example.com","jane@example.com","doe@example.com"} };
string[][]
as a jagged array
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array-of-arrays."
whereas string[,]
is a multidimensional array which
Arrays can have more than one dimension
Two aspects of difference are important:
Memory-wise:
[n,m] - Saved as a long bunch of memory, as if it was [n*m] [n][m] - Saved as a simple array of size n, where each element is a pointer to an array of size m.
Access-wise:
[n,m] - To access cell i,j what really happens is that it takes the pointer of the [n*m] array and offsets it by n*width+m, and then accesses the value. [n][m] - To access cell i,j you just access the sub-array pointer at index n (offset #1), and then access the sub-array at index m (offset #1).
[][] Is better in both aspects. More efficient in access and more flexible in memory. Also, you can access a single row just once and process it even more efficiently, since you don't perform full multidimensional access for each cell in that row.
There is, however, an advantage of [,]: You always know that the width of the matrix is fixed. Using [][], each sub-array can have different lengths, and can even be null. This can be considered as an advantage, although the opposite can be useful as well, at times.
string[][]
(Jagged Arrays) are array of array with fixed number of rows and variable length of columns whereas string[,]
(Rectangular Arrays) is matrix with fixed number of rows and columns. There is a good discussion over there