Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Multidimensional arrays are a special type of array. They're different than jagged arrays and they have a special syntax in C#. Let's explore how they're different.

0:00
In the previous video we saw one way to create a spreadsheet by creating

0:05
nested arrays.

0:06
This is called a jagged array and

0:08
it works great when we want the inner arrays to be different lengths.

0:13
This is a common scenario for

0:15
example think about a school's class rules the outer rows.

0:18
The outer array contains the classrooms, and

0:21
the inner arrays contain the students in each classroom.

0:25
Each classroom may have a different number of students.

0:29
However, sometimes we don't need nor

0:32
want the sizes of the inner arrays to be different lengths.

0:35
These are true two dimensional arrays.

0:38
In the spreadsheet example this would mean

0:41
that every row in a spreadsheet has exactly the same number of cells.

0:45
We create a two dimensional array similar to the way a jagged array is created.

0:51
So I'll say cell open square bracket comma closing square bracket sheet

0:57
equals new cell and here we can put the dimensions of our two dimensional array.

1:03
So I'll say it has 100 rows and 10 columns.

1:08
Instead of two sets of angle brackets,

1:11
instead we have a single set of angle brackets with a comma in it.

1:15
On the right side we put both dimensions of the array at the same time.

1:20
This is more than syntactic sugar.

1:23
This is actually a single array of 1,000 items.

1:27
They're organized into dimensions, one for rows and the second for columns.

1:32
If we get the value of the arrays length property we see that it has 1,000 items.

1:40
Remember that the length of the jagged array of arrays was 100.

1:43
Instead this is giving us the total number of items in the array.

1:49
The C# Repl and Mono doesn't know how to print out a multi dimensional array like

1:53
this, but we can see where individual items of the array are alike so

1:58
just say sheet first index and then the second index.

2:04
And here we see that the element at index 0,0 is nil.

2:08
Just like with jagged arrays,

2:10
the elements are initialized to their types default value.

2:14
In the case of the cell class this is nil.

2:17
We still need to assign objects to each index of the array.

2:21
Again we'll need nested loops for this.

2:24
So I'll say for int rowIndex

2:29
starting at 0 while row index is less than sheet.

2:39
Now here instead of saying length we're going to use the GetLength method and

2:45
here we'll pass the dimension of the array that we'd like to get the length for.

2:51
So I'll enter in zero to get the first dimension.

2:55
And now I'll just say rowIndex++.

2:59
Now to loop through all the columns,

3:05
I'll say four int colIndex starting

3:10
at 0 colIndex less than sheet.GetLength and

3:17
you'll pass in 1 to get the length

3:22
of the second dimension and then colIndex ++.

3:30
Now here we'll say sheet rowIndex, colIndex and

3:36
set each item in the array equal to a new Cell.

3:46
Now all of the indexes in the array have been set to an instance of cell.

3:51
We can check it out by just saying sheet and let's get the item index 2, 3.

3:57
Unlike with jagged arrays, we can't use nested for

4:00
each loops to print out the entire spreadsheet.

4:03
For each loop work with jagged arrays because they're arrays of other arrays.

4:08
A multi dimensional array is actually just

4:11
one large array that's subdivided into equal sized parts.

4:15
There are no inner arrays to loop through.

4:17
So we can't do something like foreach Cell row in sheet.

4:26
This wouldn't work.

4:29
Instead to loop through a multi dimensional array we nest the for

4:33
loops just like we did up here.

4:36
Only instead of setting the values here we can do anything we want,

4:40
such as print them out.

4:42
As you can see jagged arrays are more flexible than multidimensional arrays.

4:46
So you'll find that jagged arrays are more common.

4:49
However, multidimensional arrays are useful in many scenarios.

4:53
One nice thing about multi dimensional arrays is that we can initialize

4:57
both dimensions at once to their default values.

5:01
For example an array of integers like so

5:05
where I can say int[,] matrix = new int and

5:09
then create a matrix with five rows and five columns.

5:16
Now every value in the matrix is initialized to zero.

5:20
Matrices like this are used a lot in computer science, mathematics,

5:24
computer graphics and many other fields.

5:27
In most cases, you'll find that nested arrays like jagged arrays and other types

5:31
of nested collections, are much more common than multi dimensional arrays.

5:36
So far, as we've been creating in our spreadsheet

5:39
we've only seen examples of arrays with rows and columns.

5:43
This is only two dimensions.

5:45
We can create arrays with three or

5:47
more dimensions in the same way we do with two dimensions.

5:51
One scenario for a three dimensional array is a spreadsheet,

5:54
where each cell can contain multiple values.

5:58
in the case of jagged arrays we just have three sets of square brackets like this.

6:07
Again with Jagged arrays we still need to instantiate both of these inner

6:11
dimensions here.

6:13
I'll leave this as an exercise for you.

6:15
Since we have three nested arrays we'll need

6:18
three nested loops to initialize them.

6:21
With multidimensional arrays, we can just add more columns.

6:25
So I can say int and then two commas here to create three dimensions,

6:31
and I'll call this a threeDimMatrix that's equal

6:37
to a new integer array of size five and 5,5.

6:43
Multi dimensional arrays can have a maximum of 32 dimensions.

6:48
Unless you're doing quantum physics of some sort, it's unlikely that you'll ever

6:52
need to deal with a multidimensional array with more than three dimensions though.

6:56
Jagged arrays can have as many nested arrays as we'd like.

7:00
We can even combine jagged and multidimensional arrays like this.

7:03
I'll say int, so this is an integer array

7:08
of a three dimensional multidimensional

7:13
array of a single dimension array and I'll call it yikes.

7:25
So this would be an array of five threedimensional arrays of

7:30
integer arrays.

7:32
Now we're just talking craziness though.
You need to sign up for Treehouse in order to download course files.
Sign up