2
Answers

2D array each row can have different size?

mursaleen fayyaz

mursaleen fayyaz

11y
1.2k
1
With a Jagged array, every row can have different sizes.

int[][] jagged = new int[3][];

jagged[0] = new int[2] {5,10};
jagged[1] = new int[5] {2,4,6,8,10};
jagged[2] = new int[3] {9,10,14};



Guys, any comment?
Answers (2)
0
Hemant Srivastava

Hemant Srivastava

NA 9k 2.8m 11y
Jagged arrays are faster and have different syntax.
They are faster because they use the "newarr", vector IL calls internally.
0
Vulpes

Vulpes

NA 98.3k 1.5m 11y
Even if the rows have the same length, a jagged array is sometimes preferable to a rectangular array because it's easy to extract a whole row and pass it to another method, say, for further processing.

With a rectangular array, you have to create a single dimensional array and copy elements to it for the row you're interested in.