Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-dimensional array vs. One-dimensional

This is basically a restatement of this question: Java: Multi-dimensional array vs. One-dimensional but for C#.

I have a set amount of elements that make sense to store as a grid. Should I use a array[x*y] or a array[x][y]?

EDIT: Oh, so there are one dimensional array[x*y], multidimensional array[x,y] and jagged array[x][y], and I probably want jagged?

like image 826
Zeta Two Avatar asked Mar 29 '11 16:03

Zeta Two


People also ask

What is the difference between 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. It represents multiple data items in the form of a list.

What the difference between one-dimensional and two-dimensional?

One Dimension: Once you connect two points, you get a one-dimensional object: a line segment. A line segment has one dimension: length. Two Dimensions: A flat plane or shape is two-dimensional. Its two dimensions are length and width.

Is multidimensional and two dimensional array same?

Two – dimensional array is the simplest form of a multidimensional array. We can see a two – dimensional array as an array of one-dimensional array for easier understanding.

What is a 1 dimension array?

A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array.


1 Answers

There are many advantages in C# to using jagged arrays (array[][]). They actually will often outperform multidimensional arrays.

That being said, I would personally use a multidimensional or jagged array instead of a single dimensional array, as this matches the problem space more closely. Using a one dimensional array is adding complexity to your implementation that does not provide real benefits, especially when compared to a 2D array, as internally, it's still a single block of memory.

like image 141
Reed Copsey Avatar answered Sep 19 '22 17:09

Reed Copsey