Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Arrays5:51 with Craig Dennis
Arrays are used to store multiple bits of the same type of information in a single variable
Arrays, you can store many objects in a single variable, right? 0:00 So I could create different variables, or I could make one variable, and 0:04 it could have multiple values then, okay? 0:07 The weirdest thing about arrays, and 0:10 this is crazy, you'll think it's crazy when you first start playing with them, 0:12 and you'll do a lot of fail waves, actually, from this problem. 0:15 Arrays start at zero, okay? 0:19 Normally, we're used to counting, we're like, oh, number one, two, three, four. 0:20 Arrays start at zero, and this is something that we do as humans. 0:24 I have a daughter who's not quite one yet. 0:28 So she's actually zero years old, and if you ask me how old's your daughter, 0:31 I would never say zero years old. 0:35 I would likely say how many months she is. 0:36 I might say she is in her first year, so 0:39 it's her first year but she is really zero. 0:41 So when you're talking about arrays and your talking about the first element, 0:43 if you think of that, that kind of helps me remember that zero we do that. 0:47 Like the zero, zero to one, that's the first period there, 0:52 right, so that's the first element in the raise the zero with. 0:55 We'll a look really quick at that. 0:58 And then arrays also let you know how big they are. 1:00 Okay, so you can take a look at them. 1:03 So I am talking about this, so we can walk through a for 1:05 loop really quick with arrays. 1:08 So this is kind of why I am introducing it, 1:09 I don't want to focus too much on understanding this, and 1:10 if you have questions about these afterwards we can walk through those. 1:12 So, I thought we'd do a little example with something that I 1:15 thought I knew the numbers of. 1:18 I thought the Jackson Five, 1:19 I thought there were five Jacksons, since the title of the band is the Jackson Five. 1:20 [LAUGH] It turns out there's six, which is weird. 1:24 You see when I create the array here, there's brackets, and 1:27 that's saying that it's an array. 1:30 It's saying there's multiple of these names stored in this one variable called 1:31 jacksons, and to get the first one, the first element here is Michael. 1:35 And you can access that with the zero. 1:39 So you do bracket zero to get Michael out and you do bracket two to get Tito out. 1:41 These are the best, obviously the best of the Jacksons. 1:45 Then you can see that there were six Jacksons of the Jackson 5. 1:50 Let's do a live coding example of printing out the Jacksons, and 1:55 we'll use the old school for loop that we talked about. 1:59 If we come over and look at Jackson's Old School. 2:03 Here. 2:07 So, I've gone ahead and built that example exactly. 2:11 So, I have this array. 2:13 That's sitting here. 2:14 Let's just go ahead and say, the Jackson Five are. 2:15 All right, so let's do our for loop. 2:25 So I'm going to use this i variable equals zero. 2:27 And while i is less than the length of that array, 2:32 we're gonna loop through every single one of those cuz that's gonna return that. 2:37 And then we're gonna say i plus plus. 2:41 Because we're going to increment and the end there. 2:44 So we're going to move our i forward, and then we're going to pull a Jackson out. 2:45 And we do that remember we reference the array by the index, okay? 2:51 So this number is going to keep going up and it's going to change each time, so 2:58 0 is going to pull up Michael. 3:02 For the second one, this one's gonna pull up Tito, 3:05 and it's gonna come through there, so 3:10 each iteration through there, we're gonna say %s Jackson, 3:13 and do a new line, and we're gonna push the Jackson through. 3:18 They've got a closing method there. 3:29 Any bugs? 3:33 Any bug spotters? 3:34 All right, so let's see who these Jacksons were. 3:35 And this is the old school style, right? 3:38 So Jackson's old school. 3:42 See that I have the new school there is why the tab completion is not working. 3:51 There they are. 3:57 So we got Michael, Jackie, Tito, Germaine, Marlon and Randy. 3:58 And there was a little bit of overlap. 4:01 I think what happened is somebody hit puberty and then they popped them out and 4:02 put a new, younger kid in there just to keep the pitch right. 4:06 It was probably like a pitch problem is my guess. 4:09 But there was a time when there was six of them on stage, 4:11 which is just out of this world. 4:13 So this is the old school version, right? 4:15 So this happens all the time. 4:17 You're going to to look on Stack Overflow. 4:18 You're going to see this. 4:19 You're going to see this pattern. 4:20 It's not the way you should do it. 4:21 Java gives you a newer way to do this. 4:24 But you're going to see this out there and you're going to see this elsewhere. 4:26 You're going to see this in other programming languages as well. 4:28 If you're looping through something that you know, you're gonna use all of them. 4:30 You don't want to build a statement like that, right? 4:33 Then again, cuz you want people to come in and understand what your code's doing. 4:36 You don't want people to come in and not know what that for loop is, and 4:39 just think that that's how it works. 4:42 Chris, you look like you have a question. 4:45 Something about the variable, I thought you were saying. 4:48 >> I was thinking about that. 4:51 >> Yeah? >> [LAUGH] 4:53 >> And you keep using the variable i. 4:55 >> Yeah? 4:59 >> Where does that come from? 4:59 >> Oh, you know what, that's interesting, Chris. 5:00 Thanks for asking that question. 5:02 I'm using that because that's the standard here, but, as we know, 5:05 that's probably a bad variable name, right? 5:10 Like, what is i? 5:12 Are you talking about yourself? 5:13 Is that me? 5:14 No. 5:15 It's traditionally probably for index. 5:16 However, what happened is you can nest loops. 5:19 So what happens if there's another for loop in here, what do you use? 5:22 What's the next loop? 5:25 Oh, you use j. 5:26 Why do you use j? 5:27 I don't know. 5:28 So this is probably bad practice that's been baked in to everything, right? 5:29 So if you really wanna keep track of an index, maybe call it index. 5:34 Right? 5:39 So that's, but I used i because I wanted to show you, and thank you Chris for 5:39 asking and pointing that out, because I would have totally forgotten. 5:43
You need to sign up for Treehouse in order to download course files.Sign up