Locked lesson.
About this lesson
In this lesson, we examine how to create and use Two Dimensional Arrays.
Exercise files
Download this lesson’s related exercise files.
26 - Two Dimensional Arrays.docx61.1 KB 26 - Two Dimensional Arrays SOLUTION.docx
58.4 KB
Quick reference
Two Dimensional Arrays
A Two Dimensional Array is an Array with another Array inside of it.
When to use
Use as needed.
Instructions
string[,] names = new string[2,3] {
{"John", "Tim", "Bob"},
{"Tina", "Erin", "Mary"}
};
names[0, 1]; // returns Tim
names[1, 2]; // returns Mary
Hints & tips
- Arrays can have other arrays in them!
- You can have as many dimensions as you like.
- 00:04 Okay, in this video I want to talk about multi dimensional arrays.
- 00:07 And a multi dimensional array is just an array with arrays inside of it.
- 00:12 Like I said at the beginning of this array section, you can use all your different
- 00:15 data types inside of an array, strings, integers, floats, decimals.
- 00:18 Other arrays, right?
- 00:20 So that's what we're going to look at in this video.
- 00:22 So let's come up here, let's create an array.
- 00:25 And, it's going to be a string array.
- 00:27 And the difference right off the bat is inside of here we stick a comma.
- 00:31 So this lets C sharp know, hey, we're going to be using arrays inside of arrays,
- 00:35 multi dimensional arrays, arrays with more than one dimension.
- 00:38 All right, so same thing we need to name this thing.
- 00:41 So I'm going to call it names, we'll create an array of names, right?
- 00:44 So this is going to be equal, and same thing we call new string.
- 00:51 And inside of here you define how many things are going to be in each dimension.
- 00:54 So let's have two dimensions of three things, right?
- 00:57 So if you're having trouble visualizing what this is, I'll show you right now.
- 01:01 We take our square brackets and here's where we can add things to our array,
- 01:06 and I'm going to put this on multiple lines so it's easier to read.
- 01:09 So here we'll have another set with a comma and then another set, right?.
- 01:16 So this is going to be dimension one and this is going to be dimension two.
- 01:20 Now inside here, this is another array.
- 01:22 And we can put multiple things in here, so
- 01:27 we can put say, John, Tim and let's say Bob.
- 01:32 Inside of here, maybe we want Tina,
- 01:37 Aaron and Mary, right?
- 01:41 And then outside of here,
- 01:42 we need a semicolon to end it because we always use a semicolon to end things.
- 01:46 Now, I put this all on multiple lines,
- 01:48 you could just as easily put these on one line.
- 01:51 So we can sort of play around like this, all right?
- 02:00 Right, just like this.
- 02:01 Now, of course, this is much harder to read, so
- 02:04 much better to put it on multiple lines.
- 02:09 And it's easier to understand the different dimensions.
- 02:12 So this is the first dimension.
- 02:14 This is the second dimension.
- 02:15 See we have two dimensions, one, two.
- 02:17 Inside of each dimension, we have three things.
- 02:24 One, two, three, one, two, three, that's it.
- 02:26 So this is very, handy for all sorts of different things.
- 02:29 You're going to use this a lot.
- 02:30 I know if you've never seen a multi-dimensional anything,
- 02:32 this might be a little confusing.
- 02:34 Don't put a whole lot of thought into it.
- 02:36 Don't try and figure it out,
- 02:37 too deeply, just realize It's a way to keep track of arrays inside of arrays.
- 02:42 And we'll use it more in the future and you'll get some more experience with it.
- 02:44 It'll become second nature and won't be weird at all.
- 02:46 But for now, that's just what it is.
- 02:49 So how do we actually access this thing?
- 02:51 Well, same way as before, we call names and we use our bracket but
- 02:56 inside here, we put two numbers instead of one.
- 03:00 So if we want something from the first dimension,
- 03:03 remember arrays always start at 0, so that would be 0.
- 03:06 And then what do we want from the zero thing?
- 03:09 Well, maybe we want Tim, that would be the 0,1, 1th thing.
- 03:14 So 01 this should return Tim.
- 03:17 Let's go ahead and save and run it and see.
- 03:20 Fingers crossed, sure enough Tim, right?
- 03:24 So if we want 0,0 the 0 with dimension, 0 with item that's going to be John.
- 03:30 We save this and run it.
- 03:33 Sure enough, we get John.
- 03:34 We want to access something from the other dimension.
- 03:36 That's the first dimension.
- 03:38 Remember 0, 1 0, 1, right?
- 03:40 And then inside of here say we want Arun that's 01 again that's going to be 1, 1.
- 03:47 We save this and run it.
- 03:50 Sure enough we get Arun.
- 03:52 So a little tricky but really not too bad.
- 03:56 Very similar to creating a regular array.
- 04:00 It's just it has more arrays inside of it.
- 04:02 So, like I said, this is very useful.
- 04:05 You'll use this for all sorts of things.
- 04:06 Again, don't spend a whole lot of time trying to wrap your brain
- 04:09 around the different dimensions and things like that.
- 04:12 It's just sort of a list of arrays, basically, right?
- 04:15 And that's all there is to it.
- 04:16 So that's all for this video, in the next video we'll start to look at methods.
Lesson notes are only available for subscribers.