1 00:00:00,520 --> 00:00:02,030 When working with collections, 2 00:00:02,030 --> 00:00:06,600 you may sometimes find it necessary to create a collection of collections. 3 00:00:06,600 --> 00:00:10,770 This means having a collection of items where each item in the collection 4 00:00:10,770 --> 00:00:12,525 is itself another collection. 5 00:00:12,525 --> 00:00:17,060 [SOUND] Think about how you would store the data of a spreadsheet. 6 00:00:17,060 --> 00:00:21,230 We could do this by having an array of all the rows in the spreadsheet. 7 00:00:21,230 --> 00:00:24,720 Each row is an array of the spreadsheet cells. 8 00:00:24,720 --> 00:00:29,010 This is a two dimensional array of items with the first dimension being the rows 9 00:00:29,010 --> 00:00:30,990 and the second dimension being the columns. 10 00:00:32,090 --> 00:00:34,280 Let's see what a spreadsheet looks like in code. 11 00:00:35,450 --> 00:00:39,780 First I'll create a class to represent each cell in the spreadsheet. 12 00:00:39,780 --> 00:00:44,618 So I'll say, class Cell, and in here I'll make 13 00:00:44,618 --> 00:00:49,339 a public string property and call it Contents. 14 00:00:51,471 --> 00:00:54,447 And it we'll have both a getter and a setter. 15 00:00:56,199 --> 00:01:00,350 C# has two different ways to create an array of arrays. 16 00:01:00,350 --> 00:01:04,600 The first is simply to have the type of the array be another array. 17 00:01:04,600 --> 00:01:08,780 We'll have an outer array that contains arrays inside of it. 18 00:01:08,780 --> 00:01:12,730 We can create one by starting as we always do in creating arrays, 19 00:01:12,730 --> 00:01:16,490 by first stating the type of items that it will contain. 20 00:01:16,490 --> 00:01:19,630 So in this case that'll be cells. 21 00:01:19,630 --> 00:01:25,270 By adding a pair of square brackets, we're saying that this is an array of cells. 22 00:01:25,270 --> 00:01:29,080 Now to show that there are other arrays inside of this array, 23 00:01:29,080 --> 00:01:31,890 we'll add a second pair of square brackets. 24 00:01:31,890 --> 00:01:35,660 We'll call this array of cell arrays sheet. 25 00:01:35,660 --> 00:01:40,160 Sheet is still just the variable the holds the array of arrays. 26 00:01:40,160 --> 00:01:43,270 We still haven't declared how large our spreadsheet should be. 27 00:01:44,270 --> 00:01:47,750 Let's create a spreadsheet with 100 rows. 28 00:01:47,750 --> 00:01:49,430 To instantiate the array, 29 00:01:49,430 --> 00:01:55,009 we'll set sheet equal to a new array of 100 arrays of cells. 30 00:01:56,010 --> 00:02:01,210 So I'll say equals new Cell, and 31 00:02:01,210 --> 00:02:06,330 in here, in between the first set of square brackets, I'll enter 100. 32 00:02:06,330 --> 00:02:10,370 This 100 is the number of rows in our spreadsheet. 33 00:02:10,370 --> 00:02:15,930 Just like with a normal array, we have to say how large the outer array is up front. 34 00:02:15,930 --> 00:02:20,010 We don't have to say at this point how many cells are in each row. 35 00:02:20,010 --> 00:02:21,900 In fact, we can't. 36 00:02:21,900 --> 00:02:26,313 Let's try creating a sheet with 100 rows and 10 columns here. 37 00:02:29,189 --> 00:02:35,290 If we put 10 in the second set of square brackets, we get a compiler error. 38 00:02:35,290 --> 00:02:38,990 It isn't very obvious from the compiler error what is wrong. 39 00:02:38,990 --> 00:02:43,730 The compiler thinks that we're instantiating an array and 40 00:02:43,730 --> 00:02:46,530 getting the item at index 10 and 41 00:02:46,530 --> 00:02:50,480 then trying to assign the cell to the sheet array variable. 42 00:02:51,530 --> 00:02:54,790 So when creating an array of arrays like this, 43 00:02:54,790 --> 00:02:58,130 we only specify how large the outer array is. 44 00:02:59,570 --> 00:03:01,240 Let's inspect the sheet variable now. 45 00:03:02,560 --> 00:03:06,520 If you count them all, you'll see that there are 100 nulls in the sheet array. 46 00:03:07,590 --> 00:03:09,950 Each one of these nulls represents a row. 47 00:03:11,125 --> 00:03:14,890 They're null because we haven't created the arrays for each row. 48 00:03:14,890 --> 00:03:19,220 Right now, sheet is an array of null cell arrays. 49 00:03:19,220 --> 00:03:23,630 To create the rows of the spreadsheet, we need to instantiate an array for 50 00:03:23,630 --> 00:03:25,880 each index in the sheet array. 51 00:03:25,880 --> 00:03:27,580 We can do that like so. 52 00:03:27,580 --> 00:03:30,700 Let's say we want a spreadsheet to have ten columns. 53 00:03:31,800 --> 00:03:37,050 So for the first row, I'll say that it will be 54 00:03:37,050 --> 00:03:40,890 a new Cell array of ten items. 55 00:03:42,600 --> 00:03:45,600 Now if we take a look at the sheet variable, we'll see 56 00:03:45,600 --> 00:03:50,660 that there is a second set of curly braces here, where one of the nulls used to be. 57 00:03:50,660 --> 00:03:53,290 This is the array that we just created. 58 00:03:53,290 --> 00:03:57,970 We haven't set any values for the items in the array, so they're all null too. 59 00:03:59,670 --> 00:04:03,180 But this is just one of the 100 rows. 60 00:04:03,180 --> 00:04:07,510 In order to create the entire spreadsheet, we need to do this 99 more times. 61 00:04:08,530 --> 00:04:10,480 Thank goodness for loops. 62 00:04:10,480 --> 00:04:13,450 For this, we'll need to use a for loop. 63 00:04:13,450 --> 00:04:19,032 So I'll say for(int 64 00:04:19,032 --> 00:04:26,474 rowIndex = 0; rowIndex < 65 00:04:26,474 --> 00:04:35,476 sheet.Length; rowIndex++) 66 00:04:41,248 --> 00:04:44,510 Now in here we'll create our new rows. 67 00:04:44,510 --> 00:04:48,445 So I'll say sheet[rowIndex]. 68 00:04:50,656 --> 00:04:56,016 Equals new Cell array with ten items. 69 00:04:57,598 --> 00:05:00,826 Just to remind us what we're doing here, 70 00:05:00,826 --> 00:05:05,700 I'll put a comment that says create a row of ten cells. 71 00:05:05,700 --> 00:05:11,630 Now this will create 100 cell arrays of length 10 inside of the outer sheet array. 72 00:05:12,800 --> 00:05:16,700 This is also a good place to initialize the values of the elements 73 00:05:16,700 --> 00:05:18,530 in the inner arrays. 74 00:05:18,530 --> 00:05:23,658 So inside of the for loop we'll write another for loop to do this. 75 00:05:23,658 --> 00:05:29,278 So I'll say for(int cikndex, 76 00:05:29,278 --> 00:05:36,355 for column index, while the colIndex < 77 00:05:36,355 --> 00:05:42,010 sheet(rowIndex].Length. 78 00:05:42,010 --> 00:05:46,654 So this is the length of the array that we just created here. 79 00:05:49,080 --> 00:05:53,758 Now colIndex++). 80 00:05:56,930 --> 00:06:04,875 And in here, we'll set each one the cells equal to a new instance of the Cell class. 81 00:06:04,875 --> 00:06:06,179 So I'll say, sheet 82 00:06:09,355 --> 00:06:16,910 [rowIndex][colIndex] = new Cell(). 83 00:06:20,130 --> 00:06:25,160 When a loop is inside of another loop, it's called a nested loop. 84 00:06:25,160 --> 00:06:29,090 The inner loop is nested inside of the outer loop. 85 00:06:29,090 --> 00:06:34,750 When we have nested arrays, that is many arrays nested inside of an outer array, 86 00:06:34,750 --> 00:06:39,910 like we have here, it's very common to initialize them with a nested loop. 87 00:06:41,130 --> 00:06:46,200 Now if we inspect sheet, we'll see that the sheet array 88 00:06:46,200 --> 00:06:51,860 now contains 100 arrays with 10 cell objects in each. 89 00:06:51,860 --> 00:06:55,740 In total there are 1000 cell objects here. 90 00:06:55,740 --> 00:06:58,630 We can get any individual cell like so. 91 00:06:58,630 --> 00:07:04,114 So if we wanted the eighth cell in the fifth row, 92 00:07:04,114 --> 00:07:08,377 we'd just say sheet[4][7]. 93 00:07:08,377 --> 00:07:13,200 We can loop through each item in the arrays by using nested loops. 94 00:07:13,200 --> 00:07:16,597 These could be for loops, while loops, or foreach loops. 95 00:07:17,680 --> 00:07:20,200 Let's see what nested foreach loops would look like. 96 00:07:21,620 --> 00:07:27,457 So, I'll say foreach(Cell[] array and 97 00:07:27,457 --> 00:07:32,039 we'll call this array row in sheet 98 00:07:35,087 --> 00:07:40,392 And then foreach(Cell cell in 99 00:07:40,392 --> 00:07:46,370 row), We'll print out the cell. 100 00:07:46,370 --> 00:07:47,768 So say 101 00:07:47,768 --> 00:07:56,721 System.Console.Write(cell). 102 00:08:00,984 --> 00:08:01,900 There we go. 103 00:08:01,900 --> 00:08:05,232 Actually, to make this look a little prettier, 104 00:08:05,232 --> 00:08:08,577 let's add new lines after we print out each row. 105 00:08:08,577 --> 00:08:13,864 So, We do our foreach row in sheet. 106 00:08:22,036 --> 00:08:23,264 Foreach cell in row. 107 00:08:30,909 --> 00:08:32,085 Print this cell. 108 00:08:36,817 --> 00:08:39,096 Now right here we'll print a new line. 109 00:08:39,096 --> 00:08:46,593 So I'll say System.Console.WriteLine. 110 00:08:52,454 --> 00:08:53,440 There, that looks better. 111 00:08:54,630 --> 00:08:58,430 If you look back at where we create the arrays for each row in the spreadsheet, 112 00:08:58,430 --> 00:09:04,220 you can see that we specify how long the inner array is each time we create one. 113 00:09:04,220 --> 00:09:07,810 Instead of these inner arrays all being ten items long, 114 00:09:07,810 --> 00:09:11,280 we could have made these inner arrays all different lengths. 115 00:09:11,280 --> 00:09:17,061 For example, we can set the fifth row to be an array of 13 items instead of 10. 116 00:09:17,061 --> 00:09:23,389 So I could say sheet(4) 117 00:09:23,389 --> 00:09:28,859 = new Cell[13]. 118 00:09:28,859 --> 00:09:33,568 I mentioned at the beginning of this video that there are two ways to create an array 119 00:09:33,568 --> 00:09:34,910 of arrays. 120 00:09:34,910 --> 00:09:38,940 The first way that we've seen here is called a jagged array, 121 00:09:38,940 --> 00:09:42,370 because each of the inner arrays can be different lengths. 122 00:09:42,370 --> 00:09:47,030 In the next video, we'll see a slightly streamlined way to create our spreadsheet.