Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Arrays can contain any type of object - even other arrays.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
When working with collections,
0:00
you may sometimes find it necessary to
create a collection of collections.
0:02
This means having a collection of items
where each item in the collection
0:06
is itself another collection.
0:10
[SOUND] Think about how you would
store the data of a spreadsheet.
0:12
We could do this by having an array
of all the rows in the spreadsheet.
0:17
Each row is an array of
the spreadsheet cells.
0:21
This is a two dimensional array of items
with the first dimension being the rows
0:24
and the second dimension
being the columns.
0:29
Let's see what a spreadsheet
looks like in code.
0:32
First I'll create a class to represent
each cell in the spreadsheet.
0:35
So I'll say, class Cell,
and in here I'll make
0:39
a public string property and
call it Contents.
0:44
And it we'll have both a getter and
a setter.
0:51
C# has two different ways to
create an array of arrays.
0:56
The first is simply to have the type
of the array be another array.
1:00
We'll have an outer array that
contains arrays inside of it.
1:04
We can create one by starting as
we always do in creating arrays,
1:08
by first stating the type of
items that it will contain.
1:12
So in this case that'll be cells.
1:16
By adding a pair of square brackets, we're
saying that this is an array of cells.
1:19
Now to show that there are other
arrays inside of this array,
1:25
we'll add a second pair
of square brackets.
1:29
We'll call this array
of cell arrays sheet.
1:31
Sheet is still just the variable
the holds the array of arrays.
1:35
We still haven't declared how
large our spreadsheet should be.
1:40
Let's create a spreadsheet with 100 rows.
1:44
To instantiate the array,
1:47
we'll set sheet equal to a new
array of 100 arrays of cells.
1:49
So I'll say equals new Cell, and
1:56
in here, in between the first set
of square brackets, I'll enter 100.
2:01
This 100 is the number of
rows in our spreadsheet.
2:06
Just like with a normal array, we have to
say how large the outer array is up front.
2:10
We don't have to say at this point
how many cells are in each row.
2:15
In fact, we can't.
2:20
Let's try creating a sheet with
100 rows and 10 columns here.
2:21
If we put 10 in the second set of square
brackets, we get a compiler error.
2:29
It isn't very obvious from
the compiler error what is wrong.
2:35
The compiler thinks that we're
instantiating an array and
2:38
getting the item at index 10 and
2:43
then trying to assign the cell
to the sheet array variable.
2:46
So when creating an array
of arrays like this,
2:51
we only specify how large
the outer array is.
2:54
Let's inspect the sheet variable now.
2:59
If you count them all, you'll see that
there are 100 nulls in the sheet array.
3:02
Each one of these nulls represents a row.
3:07
They're null because we haven't
created the arrays for each row.
3:11
Right now,
sheet is an array of null cell arrays.
3:14
To create the rows of the spreadsheet,
we need to instantiate an array for
3:19
each index in the sheet array.
3:23
We can do that like so.
3:25
Let's say we want a spreadsheet
to have ten columns.
3:27
So for the first row,
I'll say that it will be
3:31
a new Cell array of ten items.
3:37
Now if we take a look at
the sheet variable, we'll see
3:42
that there is a second set of curly braces
here, where one of the nulls used to be.
3:45
This is the array that we just created.
3:50
We haven't set any values for the items
in the array, so they're all null too.
3:53
But this is just one of the 100 rows.
3:59
In order to create the entire spreadsheet,
we need to do this 99 more times.
4:03
Thank goodness for loops.
4:08
For this, we'll need to use a for loop.
4:10
So I'll say for(int
4:13
rowIndex = 0; rowIndex <
4:19
sheet.Length; rowIndex++)
4:26
Now in here we'll create our new rows.
4:41
So I'll say sheet[rowIndex].
4:44
Equals new Cell array with ten items.
4:50
Just to remind us what we're doing here,
4:57
I'll put a comment that says
create a row of ten cells.
5:00
Now this will create 100 cell arrays of
length 10 inside of the outer sheet array.
5:05
This is also a good place to
initialize the values of the elements
5:12
in the inner arrays.
5:16
So inside of the for loop we'll
write another for loop to do this.
5:18
So I'll say for(int cikndex,
5:23
for column index, while the colIndex <
5:29
sheet(rowIndex].Length.
5:36
So this is the length of the array
that we just created here.
5:42
Now colIndex++).
5:49
And in here, we'll set each one the cells
equal to a new instance of the Cell class.
5:56
So I'll say, sheet
6:04
[rowIndex][colIndex] = new Cell().
6:09
When a loop is inside of another loop,
it's called a nested loop.
6:20
The inner loop is nested
inside of the outer loop.
6:25
When we have nested arrays, that is many
arrays nested inside of an outer array,
6:29
like we have here, it's very common to
initialize them with a nested loop.
6:34
Now if we inspect sheet,
we'll see that the sheet array
6:41
now contains 100 arrays with
10 cell objects in each.
6:46
In total there are 1000 cell objects here.
6:51
We can get any individual cell like so.
6:55
So if we wanted the eighth
cell in the fifth row,
6:58
we'd just say sheet[4][7].
7:04
We can loop through each item in
the arrays by using nested loops.
7:08
These could be for loops,
while loops, or foreach loops.
7:13
Let's see what nested foreach
loops would look like.
7:17
So, I'll say foreach(Cell[] array and
7:21
we'll call this array row in sheet
7:27
And then foreach(Cell cell in
7:35
row), We'll print out the cell.
7:40
So say
7:46
System.Console.Write(cell).
7:47
There we go.
8:00
Actually, to make this
look a little prettier,
8:01
let's add new lines after
we print out each row.
8:05
So, We do our foreach row in sheet.
8:08
Foreach cell in row.
8:22
Print this cell.
8:30
Now right here we'll print a new line.
8:36
So I'll say System.Console.WriteLine.
8:39
There, that looks better.
8:52
If you look back at where we create the
arrays for each row in the spreadsheet,
8:54
you can see that we specify how long the
inner array is each time we create one.
8:58
Instead of these inner arrays
all being ten items long,
9:04
we could have made these inner
arrays all different lengths.
9:07
For example, we can set the fifth row to
be an array of 13 items instead of 10.
9:11
So I could say sheet(4)
9:17
= new Cell[13].
9:23
I mentioned at the beginning of this video
that there are two ways to create an array
9:28
of arrays.
9:33
The first way that we've seen
here is called a jagged array,
9:34
because each of the inner arrays
can be different lengths.
9:38
In the next video, we'll see a slightly
streamlined way to create our spreadsheet.
9:42
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up