Multidimensional Arrays7:34 with Jeremy McLain
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.
In the previous video we saw one way to create a spreadsheet by creating 0:00 nested arrays. 0:05 This is called a jagged array and 0:06 it works great when we want the inner arrays to be different lengths. 0:08 This is a common scenario for 0:13 example think about a school's class rules the outer rows. 0:15 The outer array contains the classrooms, and 0:18 the inner arrays contain the students in each classroom. 0:21 Each classroom may have a different number of students. 0:25 However, sometimes we don't need nor 0:29 want the sizes of the inner arrays to be different lengths. 0:32 These are true two dimensional arrays. 0:35 In the spreadsheet example this would mean 0:38 that every row in a spreadsheet has exactly the same number of cells. 0:41 We create a two dimensional array similar to the way a jagged array is created. 0:45 So I'll say cell open square bracket comma closing square bracket sheet 0:51 equals new cell and here we can put the dimensions of our two dimensional array. 0:57 So I'll say it has 100 rows and 10 columns. 1:03 Instead of two sets of angle brackets, 1:08 instead we have a single set of angle brackets with a comma in it. 1:11 On the right side we put both dimensions of the array at the same time. 1:15 This is more than syntactic sugar. 1:20 This is actually a single array of 1,000 items. 1:23 They're organized into dimensions, one for rows and the second for columns. 1:27 If we get the value of the arrays length property we see that it has 1,000 items. 1:32 Remember that the length of the jagged array of arrays was 100. 1:40 Instead this is giving us the total number of items in the array. 1:43 The C# Repl and Mono doesn't know how to print out a multi dimensional array like 1:49 this, but we can see where individual items of the array are alike so 1:53 just say sheet first index and then the second index. 1:58 And here we see that the element at index 0,0 is nil. 2:04 Just like with jagged arrays, 2:08 the elements are initialized to their types default value. 2:10 In the case of the cell class this is nil. 2:14 We still need to assign objects to each index of the array. 2:17 Again we'll need nested loops for this. 2:21 So I'll say for int rowIndex 2:24 starting at 0 while row index is less than sheet. 2:29 Now here instead of saying length we're going to use the GetLength method and 2:39 here we'll pass the dimension of the array that we'd like to get the length for. 2:45 So I'll enter in zero to get the first dimension. 2:51 And now I'll just say rowIndex++. 2:55 Now to loop through all the columns, 2:59 I'll say four int colIndex starting 3:05 at 0 colIndex less than sheet.GetLength and 3:10 you'll pass in 1 to get the length 3:17 of the second dimension and then colIndex ++. 3:22 Now here we'll say sheet rowIndex, colIndex and 3:30 set each item in the array equal to a new Cell. 3:36 Now all of the indexes in the array have been set to an instance of cell. 3:46 We can check it out by just saying sheet and let's get the item index 2, 3. 3:51 Unlike with jagged arrays, we can't use nested for 3:57 each loops to print out the entire spreadsheet. 4:00 For each loop work with jagged arrays because they're arrays of other arrays. 4:03 A multi dimensional array is actually just 4:08 one large array that's subdivided into equal sized parts. 4:11 There are no inner arrays to loop through. 4:15 So we can't do something like foreach Cell row in sheet. 4:17 This wouldn't work. 4:26 Instead to loop through a multi dimensional array we nest the for 4:29 loops just like we did up here. 4:33 Only instead of setting the values here we can do anything we want, 4:36 such as print them out. 4:40 As you can see jagged arrays are more flexible than multidimensional arrays. 4:42 So you'll find that jagged arrays are more common. 4:46 However, multidimensional arrays are useful in many scenarios. 4:49 One nice thing about multi dimensional arrays is that we can initialize 4:53 both dimensions at once to their default values. 4:57 For example an array of integers like so 5:01 where I can say int[,] matrix = new int and 5:05 then create a matrix with five rows and five columns. 5:09 Now every value in the matrix is initialized to zero. 5:16 Matrices like this are used a lot in computer science, mathematics, 5:20 computer graphics and many other fields. 5:24 In most cases, you'll find that nested arrays like jagged arrays and other types 5:27 of nested collections, are much more common than multi dimensional arrays. 5:31 So far, as we've been creating in our spreadsheet 5:36 we've only seen examples of arrays with rows and columns. 5:39 This is only two dimensions. 5:43 We can create arrays with three or 5:45 more dimensions in the same way we do with two dimensions. 5:47 One scenario for a three dimensional array is a spreadsheet, 5:51 where each cell can contain multiple values. 5:54 in the case of jagged arrays we just have three sets of square brackets like this. 5:58 Again with Jagged arrays we still need to instantiate both of these inner 6:07 dimensions here. 6:11 I'll leave this as an exercise for you. 6:13 Since we have three nested arrays we'll need 6:15 three nested loops to initialize them. 6:18 With multidimensional arrays, we can just add more columns. 6:21 So I can say int and then two commas here to create three dimensions, 6:25 and I'll call this a threeDimMatrix that's equal 6:31 to a new integer array of size five and 5,5. 6:37 Multi dimensional arrays can have a maximum of 32 dimensions. 6:43 Unless you're doing quantum physics of some sort, it's unlikely that you'll ever 6:48 need to deal with a multidimensional array with more than three dimensions though. 6:52 Jagged arrays can have as many nested arrays as we'd like. 6:56 We can even combine jagged and multidimensional arrays like this. 7:00 I'll say int, so this is an integer array 7:03 of a three dimensional multidimensional 7:08 array of a single dimension array and I'll call it yikes. 7:13 So this would be an array of five three-dimensional arrays of 7:25 integer arrays. 7:30 Now we're just talking craziness though. 7:32
You need to sign up for Treehouse in order to download course files.Sign up