1 00:00:02,370 --> 00:00:09,370 2 00:00:10,359 --> 00:00:17,359 This presentation is delivered by the Stanford Center for Professional Development. 3 00:00:22,118 --> 00:00:25,868 All right, let's get started on our next great topic then. And tonight ? 4 00:00:25,868 --> 00:00:29,609 today I should say, I think it's tonight because like the lights are on; it's actually daytime. 5 00:00:29,609 --> 00:00:36,420 So today, there is a whole new concept that we're gonna be getting into called an array. Okay? 6 00:00:36,420 --> 00:00:40,700 And all an array is, is basically a way to keep track of a whole bunch of information at once. 7 00:00:40,700 --> 00:00:43,820 So what we've done so far in class is like when you wanted to keep track of information you 8 00:00:43,820 --> 00:00:45,640 had some variable, and you had like an int, or you 9 00:00:45,640 --> 00:00:50,198 had a double, or you had a g-oval, or whatever it was. And it was you kept track of some 10 00:00:50,198 --> 00:00:53,909 small piece of information, but in the real world, really want you want to do is keep track of 11 00:00:53,909 --> 00:00:56,919 a whole bunch of information. Like say you're gonna take your midterm, 12 00:00:56,920 --> 00:01:00,800 we would want to keep track of all of your midterm scores. And it would be a real pain, 13 00:01:00,799 --> 00:01:07,179 for example, to say int Midterm 1, int Midterm 2, int Midterm 3. You can just 14 00:01:07,180 --> 00:01:11,439 see how monotonous it is. And by the time I got to ?int Midterm 150 you'd be 15 00:01:11,438 --> 00:01:15,048 ready to kill, but we couldn't stop then because we'd have to go up to like int 16 00:01:15,049 --> 00:01:17,170 Midterm 325. 17 00:01:17,170 --> 00:01:20,570 Right? And so we just don't want to do that and computers are really good at dealing with 18 00:01:20,569 --> 00:01:23,679 lots of information. So the way they allow us to keep track of a whole bunch of information 19 00:01:23,680 --> 00:01:24,240 at once 20 00:01:24,239 --> 00:01:30,069 is something called an array. And so the two key concepts for an array is that an array is ordered, so 21 00:01:30,069 --> 00:01:34,188 it has some natural ordering, and that means we can refer to the elements of the array 22 00:01:34,188 --> 00:01:36,849 using some index based on that ordering, 23 00:01:36,849 --> 00:01:43,429 and it's also homogeneous, which means that 24 00:01:43,430 --> 00:01:48,049 everything in the array that we store is of the same type. So we can have an array, for 25 00:01:48,049 --> 00:01:51,670 example, that stores integers, and we could say, "Hey, we want an array that stores 500 26 00:01:51,670 --> 00:01:52,329 integers." 27 00:01:52,329 --> 00:01:56,019 But it's only gonna store integers; it's not gonna store a couple integers and a couple doubles 28 00:01:56,019 --> 00:01:59,358 and some strings. All the types are the same so it's homogeneous. 29 00:01:59,358 --> 00:02:00,919 Okay? So 30 00:02:00,920 --> 00:02:04,049 how do we actually create one of these things? We'll 31 00:02:04,049 --> 00:02:08,259 erase some announcements. We'll slide this puppy over 32 00:02:08,258 --> 00:02:11,518 here. It's actually the first time I'm sliding boards around. 33 00:02:11,519 --> 00:02:13,050 And the reason why I always 34 00:02:13,050 --> 00:02:17,219 hesitate to slide boards around is all it takes is one really nasty time to get your finger 35 00:02:17,219 --> 00:02:18,278 crushed by a board, 36 00:02:18,278 --> 00:02:21,748 and you never want to slide boards around again because you can't write for a couple 37 00:02:21,748 --> 00:02:26,929 days and it's real fun to do class, especially for you with the left hand. All right, so 38 00:02:26,929 --> 00:02:31,239 how do we actually declare an array? First, we specify the type of the thing we want to have in that array. 39 00:02:31,239 --> 00:02:31,789 40 00:02:31,789 --> 00:02:36,019 Then we have an open bracket and closed bracket. That's gonna indicate to the computer that 41 00:02:36,020 --> 00:02:37,230 this is an array 42 00:02:37,229 --> 00:02:38,780 of this type int. 43 00:02:38,780 --> 00:02:42,879 Then we give it a name, so let's just call this ? I'm gonna call it My ARR, 44 00:02:42,879 --> 00:02:44,808 for My Array. Okay? 45 00:02:44,808 --> 00:02:48,370 And then what we need to do is basically tell the machine, "Hey, 46 00:02:48,370 --> 00:02:51,218 create some space for the array for me." Okay? 47 00:02:51,218 --> 00:02:54,658 So the way we specify that is we use our friend new, 48 00:02:54,658 --> 00:02:58,598 then we say the name of the type again, which may seem a little bit redundant because we 49 00:02:58,598 --> 00:02:59,888 just said it over here. 50 00:02:59,889 --> 00:03:03,060 But the reason why we have to say it over here again is because when you tell the machine, 51 00:03:03,060 --> 00:03:05,689 "Hey, I want you to setup some space for some integers, 52 00:03:05,689 --> 00:03:09,829 and the place I'm assigning that to is an array of integers." Okay? So these two 53 00:03:09,829 --> 00:03:12,979 types over here actually generally need to match, but you need to have that 54 00:03:12,979 --> 00:03:13,810 redundancy. 55 00:03:13,810 --> 00:03:16,868 And then the thing that we specify over here 56 00:03:16,868 --> 00:03:21,998 is we actually specify what the size is of our array. So for example, we could have five. 57 00:03:21,998 --> 00:03:25,019 So what this is actually giving to us is it's giving us an 58 00:03:25,020 --> 00:03:29,590 array of five integers. Okay? And the way you can actually think about this is 59 00:03:29,590 --> 00:03:34,019 an array is basically like a big list that has a bunch of cells in it, or what we refer 60 00:03:34,019 --> 00:03:36,509 to as the elements of 61 00:03:36,508 --> 00:03:39,028 that array. Okay? So this is my array, 62 00:03:39,028 --> 00:03:42,150 My ARR, and it's spaced for five integers. 63 00:03:42,150 --> 00:03:46,370 And the way we refer to these particular integers, or the elements of the array, and each one of these 64 00:03:46,370 --> 00:03:48,019 boxes hold one integer, 65 00:03:48,019 --> 00:03:52,549 is they're indexed because this is an ordered collection. Right? It's homogeneous because they're all integers, it's ordered, 66 00:03:52,549 --> 00:03:57,579 and it's indexed starting from zero. Because as computer scientists, we always start counting from 67 00:03:57,579 --> 00:03:58,170 zero. 68 00:03:58,169 --> 00:04:00,649 So if I ask for an array of five elements, 69 00:04:00,650 --> 00:04:04,919 they're actually indexed from zero up to the size of the array minus one. 70 00:04:04,919 --> 00:04:08,338 [Inaudible]. Just like a string, right, like the characters in a string, where zero up to length of the string 71 00:04:08,338 --> 00:04:09,409 minus one. 72 00:04:09,409 --> 00:04:10,659 Same idea with an array. 73 00:04:10,659 --> 00:04:15,199 And each one of these things in it is storing an integer. And when you create a new array, 74 00:04:15,199 --> 00:04:19,750 the values actually in the array get initialized. They get initialized to whatever the default 75 00:04:19,750 --> 00:04:21,358 is for that type. 76 00:04:21,358 --> 00:04:22,229 What does that mean? 77 00:04:22,230 --> 00:04:25,259 Well, it means for things like integers, the default value is zero. 78 00:04:25,259 --> 00:04:28,990 For things like Booleans, the default value if false. And there's a whole list of what the default 79 00:04:28,990 --> 00:04:29,840 values are, 80 00:04:29,839 --> 00:04:33,119 but we're gonna just stick with integers right now, so all these things start with the 81 00:04:33,120 --> 00:04:34,360 value zero in them. 82 00:04:34,360 --> 00:04:37,879 One of the niceties, Java gives you the arrays in this life. So if you've worked with other languages, like C 83 00:04:37,879 --> 00:04:39,259 or C++, 84 00:04:39,259 --> 00:04:42,849 it's one of those things you have to just let go because the syntax is different, the initialization 85 00:04:42,850 --> 00:04:49,750 is different, let go of your old C ways and just adopt the happiness that is Java. Okay? So 86 00:04:49,750 --> 00:04:53,598 how do I refer to these individual elements? The way I refer to the individual elements is through the 87 00:04:53,598 --> 00:04:55,009 indexes. Okay? 88 00:04:55,009 --> 00:04:59,229 So I could say something like My ARR 89 00:04:59,228 --> 00:05:03,988 zero; and I put this inside square brackets, so it's square brackets over here and square brackets 90 00:05:03,988 --> 00:05:08,668 over here. And what this is now referring to is this particular cell, which stores one integer. 91 00:05:08,668 --> 00:05:12,529 Which means I can use that just like would use a variable of type int 92 00:05:12,529 --> 00:05:14,038 anywhere else in my program. I 93 00:05:14,038 --> 00:05:18,868 can assign to it, I can use them in use them in expressions, whatever the case may be. So if I say my array equals 94 00:05:18,869 --> 00:05:20,209 five, 95 00:05:20,209 --> 00:05:24,879 what it's actually doing is just sticking the value five into this cell. Okay? So 96 00:05:24,879 --> 00:05:27,009 any questions about that? 97 00:05:27,009 --> 00:05:29,429 Hopefully fairly straightforward? All right. 98 00:05:29,428 --> 00:05:31,939 So the thing you want to think about 99 00:05:31,939 --> 00:05:35,319 for these arrays, and what makes them kind of funky, is first of all I get a whole 100 00:05:35,319 --> 00:05:38,139 bunch of values very quickly just by specifying 101 00:05:38,139 --> 00:05:39,639 the size of the array. 102 00:05:39,639 --> 00:05:42,600 The other thing is that this little index that's in here doesn't necessarily have to 103 00:05:42,600 --> 00:05:46,629 be a constant value like zero. I could actually put a variable in there. So 104 00:05:46,629 --> 00:05:51,539 I could, for example, have a for-loop that goes through all the elements in my array: 105 00:05:51,538 --> 00:05:54,769 for int I equals zero. 106 00:05:54,769 --> 00:05:58,389 I, in this case, I'm just gonna size is less than five, which is the size but we'll get 107 00:05:58,389 --> 00:06:00,960 to how we actually want to deal with that in just second. 108 00:06:00,959 --> 00:06:02,968 I-plus-plus 109 00:06:02,968 --> 00:06:05,248 and then inside here I'd say 110 00:06:05,249 --> 00:06:10,389 my array sub I which is that particular I element. Maybe I want to read this, for example, 111 00:06:10,389 --> 00:06:14,588 from the user: equals read int, 112 00:06:14,588 --> 00:06:18,879 and I'll just, you know, put the little question mark to ask the user for a value. And so what this does is 113 00:06:18,879 --> 00:06:22,588 it goes through and essentially asks the user for five integer values, 114 00:06:22,588 --> 00:06:26,639 and stores them in the indexes of the array from zero up to four. Okay? 115 00:06:26,639 --> 00:06:28,180 So one quick loop 116 00:06:28,180 --> 00:06:31,740 does all the work, right? If I have 300 values, I change this to a 300, 117 00:06:31,740 --> 00:06:34,710 I change this to a 300. I'd be good to go, that's all I need to change in my 118 00:06:34,709 --> 00:06:35,448 program. So 119 00:06:35,449 --> 00:06:37,840 that's kinda the power of arrays. Okay? 120 00:06:37,839 --> 00:06:41,299 So the more general form of arrays, this is how we might actually use them and in this 121 00:06:41,300 --> 00:06:43,340 case, I'm just thinking about integer arrays. 122 00:06:43,339 --> 00:06:48,198 The more general form of thinking about arrays is 123 00:06:48,199 --> 00:06:51,759 sort of like this. Ooh, a lot of chalk dust. You'll 124 00:06:51,759 --> 00:06:55,810 notice here, we'll just map to kind of general form. This is the type that you want 125 00:06:55,810 --> 00:06:57,418 to specify. Okay? 126 00:06:57,418 --> 00:07:02,408 Then we have open bracket, close bracket, your angle, then you have the name of your array. 127 00:07:02,408 --> 00:07:08,310 Then you say equals new, and you specify the same type again, and end size brackets 128 00:07:08,310 --> 00:07:12,500 you have the size, so that's kind of the general form. So we could have an array of doubles, 129 00:07:12,500 --> 00:07:13,500 right? We could have 130 00:07:13,500 --> 00:07:18,360 an array of Booleans. We could have an array of strings, for example, by saying string bracket-bracket. 131 00:07:18,360 --> 00:07:22,619 I'll call this s-list for string list equals new 132 00:07:22,619 --> 00:07:27,129 string. Oh, let's say I want 100 strings. Okay? 133 00:07:27,129 --> 00:07:29,399 Life is pretty easy. All right? 134 00:07:29,399 --> 00:07:32,079 So any questions about that? 135 00:07:32,079 --> 00:07:34,269 All right. Hopefully this is all fairly straightforward. 136 00:07:34,269 --> 00:07:37,430 Now the other thing that's kinda easy is one ? or that I should mention is 137 00:07:37,430 --> 00:07:38,400 you can have, 138 00:07:38,399 --> 00:07:42,418 as you might notice, a string is an object. I could have an array, for example, of g-ovals, 139 00:07:42,418 --> 00:07:43,810 or g-rects. 140 00:07:43,810 --> 00:07:47,629 So I could actually say something like 141 00:07:47,629 --> 00:07:49,868 g-oval, maybe is my type, 142 00:07:49,869 --> 00:07:53,279 and then I'm gonna have an array of these things. And I'll call this, you know, my circles 143 00:07:53,278 --> 00:07:57,860 because I'm just gonna have a whole bunch of these ovals that maybe represent circles. 144 00:07:57,860 --> 00:08:00,939 And then I'll say new 145 00:08:00,939 --> 00:08:05,009 g-oval. And here's where things get a little funky. I say new g-oval, and then let's say 146 00:08:05,009 --> 00:08:07,709 I wanna have ten of these things, I put a ten. 147 00:08:07,709 --> 00:08:11,219 Notice this looks different, even though I use the new it's different than the syntax 148 00:08:11,220 --> 00:08:14,620 for creating one new oval. Right? 149 00:08:14,620 --> 00:08:20,019 If I wanted to create one new oval, I would say something like, I'll put it down here, g-oval, and 150 00:08:20,019 --> 00:08:22,430 I'll just call this O for oval, 151 00:08:22,430 --> 00:08:28,150 equals new g-oval. And then I could give it the parameters for the constructor, like 152 00:08:28,149 --> 00:08:33,409 some original X, Y location and some size, maybe 100-comma-100. 153 00:08:33,409 --> 00:08:39,129 What this is doing is creating one object of type g-oval and assigning it to the value O. 154 00:08:39,129 --> 00:08:43,250 What this is doing is creating space for ten g-ovals, 155 00:08:43,250 --> 00:08:45,379 none of which have been created yet. 156 00:08:45,379 --> 00:08:48,649 Okay? So what does that actually mean? Let me make this a little bit smaller so I don't have 157 00:08:48,649 --> 00:08:51,100 to draw ten boxes; I'm gonna make this four, 158 00:08:51,100 --> 00:08:52,389 okay, but same effect. 159 00:08:52,389 --> 00:08:57,689 When I do this declaration, what I now have is this thing called circles, which is an array. 160 00:08:57,690 --> 00:09:02,590 It's an array that's got four cells in it. Okay? 161 00:09:02,590 --> 00:09:06,259 And each one of these cells is basically gonna be able to store a g-oval. 162 00:09:06,259 --> 00:09:10,500 But each one of these cells right now doesn't actually have a g-oval in it. Why: because I haven't 163 00:09:10,500 --> 00:09:13,419 created a new g-oval in each one of those cells. 164 00:09:13,419 --> 00:09:18,399 Each one of these cells, or elements, all it has in it is the ability to store a g-oval. 165 00:09:18,399 --> 00:09:22,019 It's kind of like if you just said g-oval, 166 00:09:22,019 --> 00:09:23,460 and I'll call this 167 00:09:23,460 --> 00:09:25,200 empty. 168 00:09:25,200 --> 00:09:28,290 Okay? What happens when I say g-oval empty? 169 00:09:28,289 --> 00:09:31,339 I get this thing called empty over here, 170 00:09:31,340 --> 00:09:36,100 which is space for a g-oval but I haven't done new on it yet which means this thing doesn't 171 00:09:36,100 --> 00:09:40,009 actually point to any object that is a g-oval. Its value, 172 00:09:40,009 --> 00:09:40,950 for example, 173 00:09:40,950 --> 00:09:42,000 is null. 174 00:09:42,000 --> 00:09:44,399 Right? There's no actual g-oval there. 175 00:09:44,399 --> 00:09:48,230 I just say, "Hey, get me the box that's gonna store a g-oval in it," but I'm not actually putting any g-oval in 176 00:09:48,230 --> 00:09:49,409 it yet. 177 00:09:49,409 --> 00:09:53,299 So when I do this declaration, I'm essentially doing the same thing as this except rather 178 00:09:53,299 --> 00:09:58,019 than getting one box, I'm getting for boxes. So each one of these boxes to begin with has a null 179 00:09:58,019 --> 00:10:01,329 in it, okay? 180 00:10:01,330 --> 00:10:05,920 So I have space to store four g-ovals but I have not yet constructed the g-ovals. 181 00:10:05,919 --> 00:10:10,068 So if I actually want to construct the g-ovals, I'm just gonna make use of this little line 182 00:10:10,068 --> 00:10:16,039 right here. I could say, for example, circles sub zero equals new g-oval. 183 00:10:16,039 --> 00:10:19,870 And what this will now do is say, "Hey, this is circle sub zero over here. 184 00:10:19,870 --> 00:10:22,039 Go and create some new oval 185 00:10:22,039 --> 00:10:24,860 and stick it in that box." 186 00:10:24,860 --> 00:10:27,230 So now, it actually has an oval to deal with. 187 00:10:27,230 --> 00:10:30,529 Okay? Or if you want to think about it from the memory point of view, 188 00:10:30,529 --> 00:10:34,009 what really is happening is this is some pointer somewhere 189 00:10:34,009 --> 00:10:37,930 that points off to some piece of memory that's storing the information 190 00:10:37,929 --> 00:10:39,870 for an oval. Okay? 191 00:10:39,870 --> 00:10:44,009 So any questions about that? That's a critical distinction between creating the array, 192 00:10:44,009 --> 00:10:48,850 which is just creating the space for essentially these pointers or references to objects, and actually 193 00:10:48,850 --> 00:10:52,699 creating the object. Which you still need to do, you can just create objects in each one 194 00:10:52,698 --> 00:10:55,209 of the individual elements of the array if you want to. 195 00:10:55,210 --> 00:10:58,170 But if you just create the array, you don't actually get space 196 00:10:58,169 --> 00:11:02,299 for the objects themselves, you just get space for the references to the objects. Much in 197 00:11:02,299 --> 00:11:09,299 the same way you do when you just declare a variable of some object type. Uh huh? Is there a more efficient way [inaudible] an array [inaudible] spaces to [inaudible]? 198 00:11:11,740 --> 00:11:14,830 To create new ovals, for example? Yeah, [inaudible]. 199 00:11:14,830 --> 00:11:16,129 Absolutely. 200 00:11:16,129 --> 00:11:22,470 So let me erase this and I will just put in one extra line up here four int I equals 201 00:11:22,470 --> 00:11:25,850 zero, I less than, in this case, four 202 00:11:25,850 --> 00:11:28,570 I-plus-plus 203 00:11:28,570 --> 00:11:30,570 circle sub-I 204 00:11:30,570 --> 00:11:32,370 equals new g-oval. 205 00:11:32,370 --> 00:11:34,980 So that's the beauty of arrays, right, is the fact that 206 00:11:34,980 --> 00:11:39,240 you can use them with objects just in the same way you could do them with ints. 207 00:11:39,240 --> 00:11:42,789 And so, you can go through and, for example, construct all your objects and now you've 208 00:11:42,789 --> 00:11:45,169 created a whole ? all these ovals are the same, 209 00:11:45,169 --> 00:11:48,829 you know, size. Right? They're add zero-zero and they size 100-comma-100 210 00:11:48,830 --> 00:11:53,450 but they're all distinct ovals. So what I've actually gotten in here is this points off to some oval and this 211 00:11:53,450 --> 00:11:57,500 points off to some oval and this, and they're all distinct ovals. They just happen to have the same 212 00:11:57,500 --> 00:11:59,190 dimensions. Okay? 213 00:11:59,190 --> 00:12:06,190 But that's one way I can create all the objects very quickly if I actually want. Uh huh? [Inaudible] ovals and then array [inaudible] oval [inaudible] array [inaudible] refer to it [inaudible]? 214 00:12:13,370 --> 00:12:16,789 Yeah, so once I've created all the ovals, I have all these pointers. You're saying like 215 00:12:16,789 --> 00:12:18,519 if I pass it to a function? 216 00:12:18,519 --> 00:12:22,828 Yeah, if I ? the thing about object is or any time I pass an object, I'm passing a reference. 217 00:12:22,828 --> 00:12:24,879 So 218 00:12:24,879 --> 00:12:29,340 if I change the object that I'm referring to, I'll always be changing the real actual underlying 219 00:12:29,340 --> 00:12:30,389 object. 220 00:12:30,389 --> 00:12:33,789 The important thing that we'll get to in, oh, about ten minutes, is that fact that when 221 00:12:33,789 --> 00:12:37,279 we get into primitive types in arrays, I can actually pass an array of primitive types 222 00:12:37,279 --> 00:12:39,169 and change the primitives types in the arrays, 223 00:12:39,169 --> 00:12:40,879 which I couldn't do before. 224 00:12:40,879 --> 00:12:44,340 So that's part of the beauty of arrays. Okay? So 225 00:12:44,340 --> 00:12:48,430 I'm gonna go ? give you a brief digression on one topic before you kinda launch into the next 226 00:12:48,429 --> 00:12:50,279 thing. And this brief digression 227 00:12:50,279 --> 00:12:54,529 is this operator that you've actually been using the whole time with tact and verb, as a 228 00:12:54,529 --> 00:12:56,970 matter of fact, we just used it on that board over there. 229 00:12:56,970 --> 00:13:00,840 And now you're old enough to sort of understand what it really does. You're 230 00:13:00,840 --> 00:13:02,170 like, "But Mehran, 231 00:13:02,169 --> 00:13:06,459 I thought I knew what it did," this little operator called plus-plus. "It was nice, I just 232 00:13:06,460 --> 00:13:10,069 added one, I used it since the days of KARO when I had for loops in KARO, 233 00:13:10,068 --> 00:13:11,929 I had a plus-plus. I didn't know what it did then. 234 00:13:11,929 --> 00:13:15,269 Then you told me what it did and life was good. I could add one to things." 235 00:13:15,269 --> 00:13:17,308 Now I'm gonna tell you what it really does and you're like, 236 00:13:17,308 --> 00:13:21,409 "It does something besides add one to something?" Yeah, it's this very tiny difference. 237 00:13:21,409 --> 00:13:25,209 The thing with this plus-plus is there's actually a bunch of different places I can put it. 238 00:13:25,210 --> 00:13:26,019 And you're like, 239 00:13:26,019 --> 00:13:29,870 "Yeah, I can imagine some places I'd want to put it, Mehran." Yeah, 240 00:13:29,870 --> 00:13:32,299 no it's not going there. All right? 241 00:13:32,299 --> 00:13:35,250 So here is X equals five. That's 242 00:13:35,250 --> 00:13:36,879 just terrible and 243 00:13:36,879 --> 00:13:39,639 it's on video. All right, so 244 00:13:39,639 --> 00:13:42,340 I have X, it has the value five. 245 00:13:42,340 --> 00:13:46,139 Now somewhere in my program, I come along and I say, "You know 246 00:13:46,139 --> 00:13:49,379 what?" Let me draw my X five over here so I can have more of my program. My 247 00:13:49,379 --> 00:13:52,409 X five, I say X-plus-plus. 248 00:13:52,409 --> 00:13:56,699 What is that doing? It turns out plus-plus is actually a method. And you're like, 249 00:13:56,700 --> 00:13:59,579 "Whoa, it's a method?" Yeah, and it returns something. 250 00:13:59,578 --> 00:14:01,359 And you're like, "Oh that's even weirder." 251 00:14:01,360 --> 00:14:04,639 So what it does is it adds one to this variable, okay, so 252 00:14:04,639 --> 00:14:06,649 it turns it into six. 253 00:14:06,649 --> 00:14:10,440 But this is actually returning a value, and the value it's returning 254 00:14:10,440 --> 00:14:16,190 is the value of variable before you did the plus-plus so it returns a five. Okay? 255 00:14:16,190 --> 00:14:20,520 In the past, we just never used that value. We said like I-plus-plus and it added one to I and said, 256 00:14:20,519 --> 00:14:23,769 "Oh here's your return value," and we said, "Yeah, we're not assigning that anywhere, we don't actually 257 00:14:23,769 --> 00:14:24,779 care." 258 00:14:24,779 --> 00:14:27,600 But we could actually assign it somewhere, okay? 259 00:14:27,600 --> 00:14:31,100 So I could actually do something like say 260 00:14:31,100 --> 00:14:34,230 int Y equals X-plus-plus, 261 00:14:34,230 --> 00:14:39,610 and so Y will get the value five, X will start off as five, I'll add one to it, it'll become 262 00:14:39,610 --> 00:14:43,740 six. And you might think, "Hey, isn't this whole thing six now? Doesn't that get assigned to Y?" 263 00:14:43,740 --> 00:14:47,948 No. When I put the plus-plus after a variable, I get back the old value of the variable. 264 00:14:47,948 --> 00:14:52,939 This form is what we refer to as a post-increment. 265 00:14:52,940 --> 00:14:57,830 Why is it post-increments, because the plus-plus comes after, that's where the post comes from, and it 266 00:14:57,830 --> 00:14:59,589 adds one, so increments. 267 00:14:59,589 --> 00:15:04,310 So the way you can think of it is I add one but after I return a value. Okay? 268 00:15:04,309 --> 00:15:08,559 So you might say, "Hey Mehran, if there's a post-increment isn't there also a pre-increment?" 269 00:15:08,559 --> 00:15:09,839 And you would be right. 270 00:15:09,840 --> 00:15:11,910 And the way a pre-increment looks 271 00:15:11,909 --> 00:15:17,019 is we take our friend the plus-plus and we put it before the variable. So we say plus-plus 272 00:15:17,019 --> 00:15:19,730 X. Okay? 273 00:15:19,730 --> 00:15:24,769 In terms of the effect it has on X, it's exactly the same; it adds one to X. So when 274 00:15:24,769 --> 00:15:28,110 I say X equals five, I put in the five here, then I say ? 275 00:15:28,110 --> 00:15:30,320 That looks like just a whole bunch of ? let 276 00:15:30,320 --> 00:15:32,970 me make that all a bit more clear; I just looked at that and I'm like, "I can't 277 00:15:32,970 --> 00:15:36,050 even read what that is." Plus-plus X, 278 00:15:36,049 --> 00:15:39,469 so what it does is it adds one to X 279 00:15:39,470 --> 00:15:44,330 and it returns the value after adding one. So it actually returns six and that's what 280 00:15:44,330 --> 00:15:45,620 gets assigned to Y. 281 00:15:45,620 --> 00:15:50,799 And this is called a pre-increment. And the reason why it's called pre-increment is I do the incrementing 282 00:15:50,799 --> 00:15:54,929 first; I add the one then I return the new value. Okay? 283 00:15:54,929 --> 00:15:57,189 So any questions about that? 284 00:15:57,190 --> 00:15:59,930 And you're like, "But Mehran, why are you telling me about this?" 285 00:15:59,929 --> 00:16:02,539 And the reason why I'm telling you about this is that 286 00:16:02,539 --> 00:16:07,829 this little form, this X-plus-plus form and the fact that it returns a value, 287 00:16:07,830 --> 00:16:11,540 actually gets used a whole bunch with arrays. And I'll show you some examples of that, but 288 00:16:11,539 --> 00:16:14,428 you need to ? that's why you need to understand what it's actually doing. 289 00:16:14,428 --> 00:16:18,049 It's adding one to the value X but returning the old value when I do the 290 00:16:18,049 --> 00:16:21,248 post-increment. And that's the version you usually see with arrays. 291 00:16:21,249 --> 00:16:22,430 Okay? But now you know. So 292 00:16:22,429 --> 00:16:27,599 any questions about post-increment versus pre-increment? All righty. 293 00:16:27,600 --> 00:16:31,080 Then let's actually put a bunch of this stuff together 294 00:16:31,080 --> 00:16:33,650 into a slightly larger program. 295 00:16:33,649 --> 00:16:37,199 And as part of that slightly larger program, we can discuss a couple more concepts that 296 00:16:37,200 --> 00:16:38,500 come up with arrays. 297 00:16:38,500 --> 00:16:42,820 Arrays hopefully aren't too hard. Like in the past, every time I've taught arrays, like it seems like, 298 00:16:42,820 --> 00:16:46,270 yeah, most people generally get arrays and they generally work the way you think they 299 00:16:46,269 --> 00:16:50,840 would work, so hopefully you should be feeling like okay with arrays. If you're feeling okay 300 00:16:50,840 --> 00:16:52,720 with arrays, nod your head. 301 00:16:52,720 --> 00:16:55,850 If you're not feeling okay with arrays, and it's okay if you're not feeling okay with arrays, shake your 302 00:16:55,850 --> 00:16:58,279 head. All righty, 303 00:16:58,279 --> 00:16:59,919 let's go on then. There's 304 00:16:59,919 --> 00:17:03,199 this notion of the actual size of the array 305 00:17:03,200 --> 00:17:07,620 versus what we refer to as the effective size of the array. 306 00:17:07,619 --> 00:17:09,119 Okay? 307 00:17:09,119 --> 00:17:13,689 The notion of the actual size of the array is this is how big I've declared the array 308 00:17:13,690 --> 00:17:18,568 to be. So when I setup my array here, the actual size is five. Okay? 309 00:17:18,568 --> 00:17:20,299 It's how big it's declared. The 310 00:17:20,299 --> 00:17:24,789 effective size is how much of it I'm really using in my program. 311 00:17:24,789 --> 00:17:29,279 So the idea is how much are you really using because sometimes what you do is you say, "Hey, 312 00:17:29,279 --> 00:17:33,039 you know what, I'm gonna have some program that computes the average midterm scores. 313 00:17:33,039 --> 00:17:36,460 But I don't actually know how many people are in this class," frightening as that may 314 00:17:36,460 --> 00:17:39,730 be, right? And I probably won't know until I see how many people actually take the midterm because 315 00:17:39,730 --> 00:17:43,160 some people might withdraw or whatever at the last minute. Hopefully you won't but sometimes 316 00:17:43,160 --> 00:17:43,990 it happens. 317 00:17:43,990 --> 00:17:47,838 So when I'm writing my program, I can't just stick in some value operator that I know. 318 00:17:47,838 --> 00:17:51,298 One thing I do know is there's probably less than 500 people taking the exam, so I can 319 00:17:51,298 --> 00:17:55,509 set my actual size to be 500. And then there's some number of midterms that we'll 320 00:17:55,509 --> 00:17:57,799 actually grade and I'll have the scores for, and that's 321 00:17:57,799 --> 00:18:00,849 how many I'm really gonna be using. That's gonna be my effective size. 322 00:18:00,849 --> 00:18:04,408 And I need to keep track of this to distinguish it from this in my program. 323 00:18:04,409 --> 00:18:07,450 So let's write a little program that actually does that. 324 00:18:07,450 --> 00:18:10,049 Oh, I think I can fit it all on this board. 325 00:18:10,049 --> 00:18:11,159 Let's try anyway. 326 00:18:11,160 --> 00:18:15,550 So what I'm gonna do is write a program that basically asks the user to enter values until 327 00:18:15,549 --> 00:18:18,109 they give some sentinel to stop. Okay? 328 00:18:18,109 --> 00:18:19,809 So my sentinel: 329 00:18:19,809 --> 00:18:21,359 private 330 00:18:21,359 --> 00:18:28,359 static final end, you have to say it kinda like you're singing a dirge, sentinel 331 00:18:30,519 --> 00:18:34,789 equals minus one. Because I really hope no one's gonna get a minus-one on the exam. I 332 00:18:34,789 --> 00:18:37,789 don't know how you would do that. It's like you don't do any of the problems and somehow you manage to 333 00:18:37,789 --> 00:18:38,309 like 334 00:18:38,309 --> 00:18:42,399 upset the instructor. Right? It's just like, "Okay, you're getting a minus-one. Thanks for playing." 335 00:18:42,400 --> 00:18:45,890 I used to get one free point if you spelled your name right, and someone 336 00:18:45,890 --> 00:18:48,059 got it wrong once so I don't do that any more. 337 00:18:48,058 --> 00:18:50,789 No joke, it really happened. I 338 00:18:50,789 --> 00:18:54,359 think just too much stress or something, or maybe I couldn't read the writing but I was like, 339 00:18:54,359 --> 00:18:56,849 "That's is not the name." All 340 00:18:56,849 --> 00:19:01,029 right. So one of the other things that we're actually gonna do is keep track of 341 00:19:01,029 --> 00:19:07,720 how large the array's gonna be in it's declared size. So I'll keep track of some maximum size, 342 00:19:07,720 --> 00:19:12,029 which I'll just call max size. And maybe I'll set max size to be equal to 500. So I 343 00:19:12,029 --> 00:19:15,200 know I'm gonna have at most 500 scores that I'm gonna store. 344 00:19:15,200 --> 00:19:19,950 And so when I'm gonna create my array, I'm gonna create an array whose size is max-size 345 00:19:19,950 --> 00:19:24,819 and then keep track of how many elements I'm actually using. So in my program, all right so let me 346 00:19:24,819 --> 00:19:27,049 put this in public 347 00:19:27,049 --> 00:19:28,200 void 348 00:19:28,200 --> 00:19:30,029 run. 349 00:19:30,029 --> 00:19:31,959 And inside here, what I'm gonna say is 350 00:19:31,959 --> 00:19:36,570 I'm gonna have some integer array that I'm gonna keep track of. Actually, there's no 351 00:19:36,569 --> 00:19:41,649 space in here, so let me just rewrite that. Some integer array and I'll call this Midterm; I'll 352 00:19:41,650 --> 00:19:44,780 just call it Mid to keep it short. It's gonna be the midterm scores. 353 00:19:44,779 --> 00:19:48,490 And what this is, is I'm going to declare, basically, a new 354 00:19:48,490 --> 00:19:55,490 array of integers that's size is this constant over here, max size. 355 00:19:55,559 --> 00:19:59,798 Okay? So I set that up and now I get an array of 500 integers 356 00:19:59,798 --> 00:20:02,670 just created for me and life is good. And I say, "Oh that's great 357 00:20:02,670 --> 00:20:06,259 but I don't know how many scores I actually have," so I want to keep track of the effective 358 00:20:06,259 --> 00:20:08,450 size to remember how many scores I have. 359 00:20:08,450 --> 00:20:10,879 So I'm gonna keep track of an extra integer in here 360 00:20:10,878 --> 00:20:12,699 called Num Scores, 361 00:20:12,700 --> 00:20:16,630 and I'm gonna set that equal to zero because to begin with I don't have any scores that the user's 362 00:20:16,630 --> 00:20:18,520 given me. Okay? 363 00:20:18,519 --> 00:20:21,769 Now I'm gonna have a while loop that's gonna read in scores from the user. 364 00:20:21,769 --> 00:20:24,019 So I might while 365 00:20:24,019 --> 00:20:28,450 true, because I'm basically keep reading scores until the user gives me the sentinel value 366 00:20:28,450 --> 00:20:31,440 over here and that indicates that I should stop, 367 00:20:31,440 --> 00:20:38,440 [inaudible] score, so I'll read one score at a time, equals read int, and I'll ask the user for a score by 368 00:20:38,740 --> 00:20:40,750 just putting in a question mark. 369 00:20:40,750 --> 00:20:44,529 Then I say if that score is equal-equal to the sentinel 370 00:20:44,529 --> 00:20:48,389 then I'm done. Right? Just gave me a negative one, I know that's not a real score so I can 371 00:20:48,390 --> 00:20:50,750 just break. 372 00:20:50,750 --> 00:20:55,690 But if they gave me a score that was not a negative one, so it's some valid score, okay, 373 00:20:55,690 --> 00:20:58,179 then I want to store that in my array. 374 00:20:58,179 --> 00:21:02,259 And so the way I'm gonna store in my array is I'm say mid 375 00:21:02,259 --> 00:21:06,490 sub, and here's where I'm gonna get funky with my plus-plus operator, 376 00:21:06,490 --> 00:21:12,220 num scores plus-plus. Let me write that a 377 00:21:12,220 --> 00:21:19,220 little more clearly. Num scores plus-plus equals score. And 378 00:21:20,779 --> 00:21:22,259 there's the end of my while loop. Okay? 379 00:21:22,259 --> 00:21:26,440 And then my program will go on after that. So what's going on here, okay? 380 00:21:26,440 --> 00:21:30,200 And the reason why I show you this is you will see this on, if you look at real Java programs, 381 00:21:30,200 --> 00:21:35,140 this kind of thing happens all the time where you do a plus-plus inside the index for an array. 382 00:21:35,140 --> 00:21:40,610 So what happens, this array gets setup, it's got size 500, so 383 00:21:40,609 --> 00:21:41,898 zero, 384 00:21:41,898 --> 00:21:48,649 one, two, three. I won't write it all out. We have a big break in here. This is 499 over here. All right? 385 00:21:48,650 --> 00:21:53,830 So we have this huge big array and num score starts off as zero. So here's num scores. 386 00:21:53,829 --> 00:21:57,449 I'll just abbreviate NS for num scores. It comes in and says read some score from 387 00:21:57,450 --> 00:21:58,679 the user, so it's get an integer. And I say, 388 00:21:58,679 --> 00:22:02,149 "Oh, someone got 100 on the exam," wonderful thing. 389 00:22:02,148 --> 00:22:06,609 Is 100 equal to minus one? No, so I don't break out of the loop. And now I'm gong set midterm 390 00:22:06,609 --> 00:22:10,229 sub num scores plus-plus to score. So what's that gonna do? 391 00:22:10,230 --> 00:22:13,640 First it's gonna come over here and say, "What score?" because it always evaluates the right-hand 392 00:22:13,640 --> 00:22:18,490 first. That score's 100, that's all you need to do. Where am I gonna store scores? 393 00:22:18,490 --> 00:22:21,930 I'm gonna store it over here. How do I evaluate this? 394 00:22:21,930 --> 00:22:26,769 I say, "Okay, I'm gonna store it somewhere in my array Mid," here's the array Mid, "which one 395 00:22:26,769 --> 00:22:28,499 of these cells am I gonna store it into?" 396 00:22:28,499 --> 00:22:33,778 I'm gonna store it into the cell given by num scores plus-plus. So what do I do? This is 397 00:22:33,778 --> 00:22:39,679 a post-increment. I add one to num scores, num scores now becomes one, 398 00:22:39,679 --> 00:22:43,750 but what value actually gets returned there from the plus-plus? Zero. 399 00:22:43,750 --> 00:22:45,700 Zero, right? That's a social, 400 00:22:45,700 --> 00:22:47,630 all around, 401 00:22:47,630 --> 00:22:50,070 except like there's three pieces of candy right there. 402 00:22:50,069 --> 00:22:55,349 It returns a zero so Mid sub-zero is what gets score, so this gets 100. 403 00:22:55,349 --> 00:23:00,288 And I've gotten the nice effect where I've gotten the score stored in the location I want, 404 00:23:00,288 --> 00:23:04,450 and at the same time I've incremented num scores to tell me how many scores in this 405 00:23:04,450 --> 00:23:05,100 array 406 00:23:05,099 --> 00:23:08,319 have actually been entered that I care about. Okay? 407 00:23:08,319 --> 00:23:10,220 That's why you see this all the time 408 00:23:10,220 --> 00:23:14,598 with arrays, especially in Java or other Java-like languages like C or C++ 409 00:23:14,598 --> 00:23:17,309 because it's a shorthand that's so common to say, "Yeah, 410 00:23:17,309 --> 00:23:21,589 the next time you get a score put it in the next available spot and increment 411 00:23:21,589 --> 00:23:24,579 the number of scores you have." So the next time I go through the loop, it gets another score. Let's 412 00:23:24,579 --> 00:23:28,199 say someone got a 30. Sorry, just didn't work out. Study. 413 00:23:28,200 --> 00:23:29,679 You get the 30; 414 00:23:29,679 --> 00:23:34,130 it's not equal to the sentinel. Where am I gonna store that? Mid sum num scores plus-plus. 415 00:23:34,130 --> 00:23:38,929 Num scores is currently one. I add one to it, I get two, but I return the old value, which 416 00:23:38,929 --> 00:23:42,680 is one, and so I stick in the 30 in Mid sub one. Okay? 417 00:23:42,680 --> 00:23:45,929 So num scores here is the effective size of my array. 418 00:23:45,929 --> 00:23:50,509 It's how many elements of that array I actually care about using. So if I want to now go compute the 419 00:23:50,509 --> 00:23:51,799 average of all the scores, 420 00:23:51,799 --> 00:23:55,200 I'm not gonna go through all 500 elements because not all 500 elements 421 00:23:55,200 --> 00:23:57,289 have valid scores that I care about. 422 00:23:57,289 --> 00:24:01,879 I'm just gonna go through num scores number of scores, the effective size of my array, even 423 00:24:01,880 --> 00:24:04,600 though I might have some declared size that's larger. Okay? 424 00:24:04,599 --> 00:24:09,459 So any questions about effective versus actual size or using this plus-plus operator to 425 00:24:09,460 --> 00:24:14,170 keep track of things? 426 00:24:14,170 --> 00:24:18,350 Uh huh? [Inaudible] num scores [inaudible]? Ah, good question. Is that what you were gonna ask back there too? 427 00:24:18,349 --> 00:24:21,909 Yeah. Yeah, what if like, you know someone just enrolled a whole bunch of times in the class. [Inaudible]. 428 00:24:21,910 --> 00:24:23,558 Yeah, I can get to you. 429 00:24:23,558 --> 00:24:25,250 That's an excellent question. 430 00:24:25,250 --> 00:24:28,210 What do you think would happen if I try to access, say, 431 00:24:28,210 --> 00:24:34,450 mid sub 500? You're like, "I don't 432 00:24:34,450 --> 00:24:35,650 know. 433 00:24:35,650 --> 00:24:39,340 I get candy?" No. You 434 00:24:39,339 --> 00:24:42,558 get an exception. Okay? 435 00:24:42,558 --> 00:24:46,910 But this time it's not a little exception, you're getting a big honking exception. 436 00:24:46,910 --> 00:24:50,950 And so if I'm getting ready to throw this out somewhere, it's gonna be bad. 437 00:24:50,950 --> 00:24:52,370 It's gonna hurt. 438 00:24:52,369 --> 00:24:56,298 Okay? So that's an exception you first of all you don't want to get, and second of all, if you get it, you 439 00:24:56,298 --> 00:24:59,609 probably don't want to deal with it in your program unless you're writing some very advanced 440 00:24:59,609 --> 00:25:00,319 program. 441 00:25:00,319 --> 00:25:03,648 You want to figure out that there was some problem with your logic and go fix the problem with the 442 00:25:03,648 --> 00:25:04,669 logic. All right? 443 00:25:04,670 --> 00:25:11,670 It's kinda like eventually someone will get this exception somewhere and they're like, "Oh. I 444 00:25:13,250 --> 00:25:18,210 am disturbed by your lack of bounds checking on the array." You know, it's just one of those things 445 00:25:18,210 --> 00:25:19,480 where you want to 446 00:25:19,480 --> 00:25:24,860 check ? Don't worry, I'm not gonna give the whole lecture like this because if I did I couldn't see after a while. 447 00:25:24,859 --> 00:25:29,259 But it's an evil, evil exception to get. It's not that bad but you should be careful of 448 00:25:29,259 --> 00:25:36,259 that exception. It's a big one not to be confused with the small exceptions. Thanks for asking. Yeah, 449 00:25:37,990 --> 00:25:41,620 it's not a plant. Valid question. Figured someone might ask it. 450 00:25:41,619 --> 00:25:46,729 So let me show you an example of how we actually deal with that in a program. 451 00:25:46,730 --> 00:25:47,599 Okay? 452 00:25:47,599 --> 00:25:52,980 So in a program, we can get around this in some slightly more interesting way 453 00:25:52,980 --> 00:25:54,380 and avoid the exception. 454 00:25:54,380 --> 00:25:55,760 So here's a 455 00:25:55,759 --> 00:26:00,930 more complicated version of the program we just wrote. Rather than setting a maximum size to 456 00:26:00,930 --> 00:26:05,589 be a size that's declared as a constant, I'm actually gonna use ? ask the user for a maximum size. 457 00:26:05,588 --> 00:26:09,019 Because let's say maybe I wanna run this program for 106A and then I wanna give the program 458 00:26:09,019 --> 00:26:12,138 to some other instructor somewhere and they can run it for their class, and they don't 459 00:26:12,138 --> 00:26:15,158 want to have recompile the program. They just want to start the program and say, 460 00:26:15,159 --> 00:26:18,570 "Yeah, I don't know how big class is either, but its maximum size 461 00:26:18,569 --> 00:26:22,089 is like 4,000," right? It's like ECON1 or something like that. 462 00:26:22,089 --> 00:26:25,808 "And so just give me an array." So here's one of the things that's interesting. 463 00:26:25,808 --> 00:26:30,490 I have int max length and I'm gonna ask the user for the maximum size of the array, rather than having 464 00:26:30,490 --> 00:26:31,759 it set as a constant. I now declare my 465 00:26:31,759 --> 00:26:35,308 array, I'll call it Midterm Scores here, 466 00:26:35,308 --> 00:26:39,480 to be a size that's this max length that's given to me by the user. 467 00:26:39,480 --> 00:26:43,620 If you've programmed in another language like C or C++, at this point you should going, 468 00:26:43,619 --> 00:26:46,879 "Oh Mehran, I couldn't do that in C or C++." 469 00:26:46,880 --> 00:26:51,340 And if you never programmed in C or C++, you should be going, "Yeah Mehran, I don't care what you can do 470 00:26:51,339 --> 00:26:53,299 in C or C++ because I'm programming in Java." 471 00:26:53,299 --> 00:26:57,419 And Java allows you to declare an array like this where the size is actually something 472 00:26:57,420 --> 00:27:01,750 that's dynamic, that's only known at the runtime when the user enters it. Okay? 473 00:27:01,750 --> 00:27:05,859 Here's my number of actual scores, that's gonna be my effective size for the array, how 474 00:27:05,859 --> 00:27:08,240 many elements of the array I actually care about. 475 00:27:08,240 --> 00:27:12,259 And now rather than having a while loop which may allow me to just potentially go off the 476 00:27:12,259 --> 00:27:13,298 end of the array, 477 00:27:13,298 --> 00:27:14,989 I'm gonna have a for loop 478 00:27:14,989 --> 00:27:18,100 and this for loop is gonna count up to the maximum length. 479 00:27:18,099 --> 00:27:21,609 So it's gonna guarantee if I ever get to the maximum size of the array, I'm 480 00:27:21,609 --> 00:27:26,808 gonna stop asking the user for any more values because I will have exhausted the for loop. But 481 00:27:26,808 --> 00:27:30,910 if I haven't yet exhausted the for loop, what I'll do inside here is I will get a score from 482 00:27:30,910 --> 00:27:33,900 the user, so I'll read in the next score into midterm score, 483 00:27:33,900 --> 00:27:37,030 if that score is the sentinel I will break out of the loop. 484 00:27:37,029 --> 00:27:41,670 If the score's not the sentinel, then I will increment my number of actual scores and iterate 485 00:27:41,670 --> 00:27:43,710 this over and over. Okay? 486 00:27:43,710 --> 00:27:48,429 Now the interesting thing here is if the user actually enters the sentinel before they get 487 00:27:48,429 --> 00:27:51,340 to filling in all the elements of the array, the max length, 488 00:27:51,339 --> 00:27:52,639 I break. 489 00:27:52,640 --> 00:27:56,530 Break always takes you out of the closest encompassing loop. So this whole time you've 490 00:27:56,529 --> 00:27:59,009 been using break with, for example, while loops. 491 00:27:59,009 --> 00:28:03,140 This will actually break out of a for loop; you can break out of for loops as well. It's 492 00:28:03,140 --> 00:28:07,690 not the greatest style in the world but sometimes in rare occasions like this, it actually makes 493 00:28:07,690 --> 00:28:11,850 sense because you wanna say, "The most I'm ever count up to is max length. Guarantee me that 494 00:28:11,849 --> 00:28:15,089 I'm gonna go any higher than that, otherwise I'm get the Darth Vader exception and 495 00:28:15,089 --> 00:28:17,319 life is gonna be bad, but 496 00:28:17,319 --> 00:28:20,990 I might break out earlier if the user actually gives me the sentinel." Okay? 497 00:28:20,990 --> 00:28:24,359 And I'll only increment num scores if they didn't give me the sentinel, so I'm not 498 00:28:24,359 --> 00:28:27,089 gonna count num score ? I'm not gonna count the sentinel 499 00:28:27,089 --> 00:28:30,428 as one of the values I actually care about. Even though it gets stored in the array, 500 00:28:30,429 --> 00:28:34,659 I'm not actually gonna say that's a valid value because I break out before I actually increment num 501 00:28:34,659 --> 00:28:36,350 scores. Okay, 502 00:28:36,349 --> 00:28:41,839 any questions about that? Uh huh? [Inaudible]. 503 00:28:41,839 --> 00:28:46,139 If they enter a negative score for the maximum? Well, try it and see what happens. All right? It's not never 504 00:28:46,140 --> 00:28:50,450 happen in real life and so if ? ooh, if you're really interested ? I might just 505 00:28:50,450 --> 00:28:53,250 peg her because it makes me angry. 506 00:28:53,250 --> 00:28:55,450 I can't reach you 507 00:28:55,450 --> 00:28:57,740 but I'm gonna keep trying. 508 00:28:57,740 --> 00:28:59,950 Try it yourself and see what happens. 509 00:28:59,950 --> 00:29:04,110 It'll probably give you some something that won't work, right? 510 00:29:04,109 --> 00:29:06,000 So what's something else that I want to do? 511 00:29:06,000 --> 00:29:07,160 And I'll get to do that 512 00:29:07,160 --> 00:29:10,350 right now but let me do it on the board before I show it to you in this program because there's 513 00:29:10,349 --> 00:29:13,679 something else I might actually want to do in the program. 514 00:29:13,680 --> 00:29:17,529 What happens if I pass an array as a parameter? Right? So this whole time we've been talking 515 00:29:17,529 --> 00:29:21,849 about arrays, we've sort of had the array inside your run method and life was good. And we were like, 516 00:29:21,849 --> 00:29:24,649 "Oh array. Yeah, I can do all this stuff with it." 517 00:29:24,650 --> 00:29:28,480 What if I want to pass an array, which is a common thing to do, as an actual parameter to 518 00:29:28,480 --> 00:29:35,480 a function? Okay? So let's say I have public void run. Inside here, I have 519 00:29:37,470 --> 00:29:43,839 int bracket bracket. I'll call it ARR, for my array, equals new int ten, so I get an array 520 00:29:43,839 --> 00:29:44,918 of ten elements. 521 00:29:44,919 --> 00:29:49,120 And now I wanna pass this array. I wanna say, "Hey Mehran, rather than reading in all the values from the 522 00:29:49,119 --> 00:29:51,750 user in your main program or in run, 523 00:29:51,750 --> 00:29:55,558 I wanna decompose the program. I want to have some function that actually reads all the values 524 00:29:55,558 --> 00:29:57,089 for me. Can I do that?" 525 00:29:57,089 --> 00:29:58,230 Yes, you can. 526 00:29:58,230 --> 00:30:03,630 And so let me have some method called read array, and what I'm going to pass to it is the 527 00:30:03,630 --> 00:30:07,429 array. When I pass an array, if I wanna pass the whole thing, 528 00:30:07,429 --> 00:30:11,989 I just specify the name of the array. I do not specify an individual index on 529 00:30:11,989 --> 00:30:12,410 530 00:30:12,410 --> 00:30:15,940 the array because if I specify an individual index I would just be passing a single element 531 00:30:15,940 --> 00:30:17,740 of the array, which would be a single int. 532 00:30:17,740 --> 00:30:20,980 If I want to pass the whole array, I give the name of the array 533 00:30:20,980 --> 00:30:22,470 with no other brackets. 534 00:30:22,470 --> 00:30:25,370 Okay? So this is gonna pass array method. So 535 00:30:25,369 --> 00:30:28,589 somewhere down here, let's just say that's the end of my run method, maybe I have some more 536 00:30:28,589 --> 00:30:32,839 stuff in here, I have private void 537 00:30:32,839 --> 00:30:39,839 read array. And read array is gonna take in some parameter. How do I specify a parameter array? 538 00:30:40,250 --> 00:30:44,819 I say int bracket bracket. That indicates the thing that is getting passed to me is an 539 00:30:44,819 --> 00:30:46,619 array of integers. Okay? 540 00:30:46,619 --> 00:30:49,989 And then I give it a name for the parameter. Here I'll call it A, 541 00:30:49,990 --> 00:30:53,190 just to distinguish it from ARR. All 542 00:30:53,190 --> 00:30:55,759 right? So inside here I get A, 543 00:30:55,759 --> 00:31:00,059 and so I'm gonna have some for loop inside here, for int I equals zero. And you might 544 00:31:00,059 --> 00:31:00,879 say, 545 00:31:00,880 --> 00:31:02,080 "Oh Mehran, 546 00:31:02,079 --> 00:31:05,788 how do you know how big the array is? How do you know how many elements to ask the user 547 00:31:05,788 --> 00:31:08,329 for," right, "because you just passed the array? 548 00:31:08,329 --> 00:31:12,369 Where is the size of the array? You didn't specify ? if you had it specified as a constant, 549 00:31:12,369 --> 00:31:13,989 like you did before with max size, 550 00:31:13,990 --> 00:31:15,909 then I could refer to that constant over here. 551 00:31:15,909 --> 00:31:19,830 But you didn't specify it with a constant, so what do I do?" 552 00:31:19,829 --> 00:31:22,599 I would normally give you concentration music and let you decide. 553 00:31:22,599 --> 00:31:26,898 But in fact arrays, and this is one of the nice things about arrays in Java, they carry around 554 00:31:26,898 --> 00:31:29,508 their size with them. Okay? 555 00:31:29,509 --> 00:31:34,680 So what I can say is array dot length. Okay? 556 00:31:34,680 --> 00:31:36,360 And the funky thing here, 557 00:31:36,359 --> 00:31:40,758 to distinguish it from strings, when you ask the string for its length you put in an 558 00:31:40,759 --> 00:31:42,960 open brace or an open bracket 559 00:31:42,960 --> 00:31:45,049 or open paren/closed paren. 560 00:31:45,049 --> 00:31:50,220 Here you don't specify open paren/closed paren. Length is actually some internal variable 561 00:31:50,220 --> 00:31:53,019 of an array that is public so it is available to you. 562 00:31:53,019 --> 00:31:58,129 So you're actually referring directly to this public variable of the array. So array dot length, 563 00:31:58,130 --> 00:31:59,710 I want to say actually I 564 00:31:59,710 --> 00:32:05,090 is less than array dot length, so let me set this to be zero. I is less than array dot length, 565 00:32:05,089 --> 00:32:06,369 I-plus-plus. 566 00:32:06,369 --> 00:32:10,599 And so if I pass arrays of different length here, their length, their declared length, right, 567 00:32:10,599 --> 00:32:12,730 I don't know it's effective size is, 568 00:32:12,730 --> 00:32:14,519 it's declared length is ten. 569 00:32:14,519 --> 00:32:17,819 And so what I might do inside here is say: A sub 570 00:32:17,819 --> 00:32:23,899 I equals read int, to get an integer from the user. 571 00:32:23,900 --> 00:32:28,040 And that's supposed to be a question mark inside double quotes. Now you might see this 572 00:32:28,039 --> 00:32:29,308 and say, 573 00:32:29,308 --> 00:32:31,069 "Oh Mehran, 574 00:32:31,069 --> 00:32:35,309 I thought you told me when I pass around primitive types I get a copy of the primitive type. Right? 575 00:32:35,309 --> 00:32:39,159 So when I pass an int, I get a copy of the int, and if I try to muck with that int, 576 00:32:39,160 --> 00:32:42,650 I'm just mucking with the copy. And so when I return to my 577 00:32:42,650 --> 00:32:48,050 calling ? the location from where I called that particular method, that value hasn't changed." 578 00:32:48,049 --> 00:32:49,908 Yeah, that's for primitive type 579 00:32:49,909 --> 00:32:53,580 not for an array, even if the array is of primitive type. 580 00:32:53,579 --> 00:32:59,168 So the thing you want to think about passing arrays is arrays always get passed by reference, 581 00:32:59,169 --> 00:33:00,970 just like object. 582 00:33:00,970 --> 00:33:06,289 If you pass an array, you're getting the actual array because it says, "Hey you know what, arrays 583 00:33:06,289 --> 00:33:10,490 are big. I don't want to copy ten integers. I'm lazy. I'm just gonna let you know where that 584 00:33:10,490 --> 00:33:14,740 array actually lives in memory." And so, when you make any changes to that array, you're actually 585 00:33:14,740 --> 00:33:18,469 changing in memory at the place where this array actually lives. 586 00:33:18,469 --> 00:33:23,058 So it does it mainly for efficiency reasons, right? Because if this was an array of a million elements, 587 00:33:23,058 --> 00:33:26,740 if you got a copy you would have to copy a million elements every time you made this 588 00:33:26,740 --> 00:33:30,888 call. Rather than copying them it says, "Hey, I'm just gonna let you know where those million elements 589 00:33:30,888 --> 00:33:34,169 live. You want to mess with them, you mess with them. You're gonna change the actual one. 590 00:33:34,170 --> 00:33:41,170 If don't want to mess with them, then don't mess with them." 591 00:33:44,339 --> 00:33:49,209 Uh huh? [Inaudible] variable is public, can you change the length [inaudible] code? You shouldn't try. I mean I just wondered if that would happen if you actually did that. 592 00:33:49,210 --> 00:33:53,630 Yeah. I wouldn't encourage you try it. I would just say don't try it; it's a bad 593 00:33:53,630 --> 00:33:56,260 time. But if you really want to see what happens, try 594 00:33:56,259 --> 00:34:03,259 it. Just it's never something you would want to do. Uh huh? Can arrays be ivars? 595 00:34:03,259 --> 00:34:07,380 Yeah, arrays can be ivars. So if you want to have an array declared inside a class and actually 596 00:34:07,380 --> 00:34:10,869 store an array inside a class, that's perfectly fine. So arrays you can use just like any other 597 00:34:10,869 --> 00:34:13,579 variables, you just get a whole bunch of stuff. Okay? 598 00:34:13,579 --> 00:34:18,259 So but the important thing is any changes you make to the array, the changes persist; 599 00:34:18,259 --> 00:34:20,898 arrays always get passed by reference. Okay? 600 00:34:20,898 --> 00:34:23,239 So at this point, you could also say, "Hey Mehran, 601 00:34:23,239 --> 00:34:26,939 that means if I wanted to change around some integer, and I just had a single integer, could I 602 00:34:26,940 --> 00:34:30,530 actually have an array of one integer and pass that array of one integer and change 603 00:34:30,530 --> 00:34:32,889 that one value, and I could change an integer that way?" 604 00:34:32,889 --> 00:34:33,829 Yeah, you could. 605 00:34:33,829 --> 00:34:38,389 It would be horrendous style but you could do it. Okay? So don't do it in this class 606 00:34:38,389 --> 00:34:41,809 and don't ever do it as a software engineer because if you do this as a software engineer, people will look at that and 607 00:34:41,809 --> 00:34:42,140 say, 608 00:34:42,139 --> 00:34:43,588 "Where did you learn this?" And you'll say, 609 00:34:43,588 --> 00:34:47,009 "Oh, that Sahami guy at Stanford told me this." And then they'll come and hunt me down, and kill 610 00:34:47,009 --> 00:34:48,519 me. 611 00:34:48,518 --> 00:34:49,778 And that's bad; at 612 00:34:49,778 --> 00:34:50,849 least for me, 613 00:34:50,849 --> 00:34:57,849 may not be for you. Don't do it. It's not how we try to pass integers by reference. Uh huh? Can you make arrays of arrays? Like if you wanted to add all of our test scores divided by [inaudible] tests. You can and 614 00:35:00,469 --> 00:35:04,889 you're actually one day ahead of me. So next time, we'll talk about multidimensional 615 00:35:04,889 --> 00:35:09,009 arrays, where you can have arrays of arrays of arrays of arrays. And it can just ? till the 616 00:35:09,009 --> 00:35:12,708 cows come home. You have like four-dimensional arrays. You can have like 12-dimensional arrays and just 617 00:35:12,708 --> 00:35:15,719 expand your view of the universe. 618 00:35:15,719 --> 00:35:17,699 It's like string theory with arrays. 619 00:35:17,699 --> 00:35:21,680 Yeah, the other dimension's just wrapped so tightly you can't actually stick integers in them, 620 00:35:21,679 --> 00:35:23,089 but that's not important right now. There's 621 00:35:23,090 --> 00:35:30,090 like a few string theory people who are like, "Ho-ho-ho, that's funny Mehran." Everyone else is like, 622 00:35:30,458 --> 00:35:37,458 "What?" All right, so not that I'm a string theorist, but I like to play one on TV. All right, so 623 00:35:37,469 --> 00:35:42,259 what else can we do with arrays? If arrays, when we pass them around we're actually passing 624 00:35:42,259 --> 00:35:46,630 references to arrays that means other funky things are sort of possible. So let me show 625 00:35:46,630 --> 00:35:49,209 you another example of something that's possible, if 626 00:35:49,208 --> 00:35:52,108 we return to our program. This one's not so funky. 627 00:35:52,108 --> 00:35:55,628 This one just says, "Hey, what I want to do is after you give me all those scores I want to compute an 628 00:35:55,628 --> 00:36:00,179 average score." So I'm gonna have some method where I want to pass to this method is 629 00:36:00,179 --> 00:36:04,710 the Midterm Scores array that I had before. Right? That's just this array that we declared up here. 630 00:36:04,710 --> 00:36:07,130 And I also want to pass to you the actual 631 00:36:07,130 --> 00:36:10,568 number of values that I'm using, the effective size of the array. 632 00:36:10,568 --> 00:36:14,949 The reason why I'm doing that is because if I don't pass you the effective size of the array, 633 00:36:14,949 --> 00:36:18,898 you don't know the effective size of the array. The only thing you know that the array gives you 634 00:36:18,898 --> 00:36:22,788 is declare its size; it doesn't keep track of, oh, which element you're using or which element 635 00:36:22,789 --> 00:36:25,269 you're not using. It says, "Hey, I know I'm this big. 636 00:36:25,268 --> 00:36:28,418 And if you're not using the whole size of me, then you do need to pass the effective 637 00:36:28,418 --> 00:36:30,299 size." So inside compute average, 638 00:36:30,300 --> 00:36:34,750 it's getting passed an array of integers and it's getting passed the act number ? actual 639 00:36:34,750 --> 00:36:36,030 scores that we care about. 640 00:36:36,030 --> 00:36:40,609 And it just goes through a loop that counts up all the actual scores, adds them to this thing called 641 00:36:40,608 --> 00:36:44,630 Average that we're sort of adding up as we go along. Right? So it's adding all the individual elements 642 00:36:44,630 --> 00:36:48,838 of the array to average. And then when we're done going through all the elements we care about, 643 00:36:48,838 --> 00:36:53,449 we just set average equal to average divided by num actual scores. And because average is 644 00:36:53,449 --> 00:36:56,389 a double, it does real value division so we get the average 645 00:36:56,389 --> 00:36:58,608 and we return the average here. Okay? 646 00:36:58,608 --> 00:37:02,449 And so all this prints out is average score and then it will actually print out what the real 647 00:37:02,449 --> 00:37:05,459 average of those scores were a sum double. Okay? 648 00:37:05,458 --> 00:37:08,868 So important thing to keep in mind, even though you get this facility where you know the 649 00:37:08,869 --> 00:37:12,380 declared size of the array, sometimes you still need to pass the actual ? 650 00:37:12,380 --> 00:37:17,470 the effective size, if you have ? if you're not using all the elements of your array. If you're 651 00:37:17,469 --> 00:37:19,779 always guaranteed that you're always gonna have, you know, 652 00:37:19,780 --> 00:37:23,190 "Yeah, this is the Olympics and there's always seven judges. And I know it's always gonna be seven and 653 00:37:23,190 --> 00:37:27,039 that's just the way it is, unless there were some kind of payoff scandal," then 654 00:37:27,039 --> 00:37:31,069 you know it's seven, the declared size and the effective size are the same. Okay? 655 00:37:31,068 --> 00:37:34,998 One of the other things I can also do ? now in this case, notice ? in this 656 00:37:34,998 --> 00:37:40,618 array example case, I didn't actually change any of the values inside the array. Okay? 657 00:37:40,619 --> 00:37:44,769 Even though I didn't change any of the values, the array still got passed by reference so I still had the real 658 00:37:44,768 --> 00:37:47,478 array. If I changed any values, they would've persisted. 659 00:37:47,478 --> 00:37:51,398 But I still get a copy no matter ? or still always get the actual array, I don't get 660 00:37:51,398 --> 00:37:55,128 a copy whenever I pass it whether or not I change the values. In most cases, you don't change the 661 00:37:55,128 --> 00:37:55,989 values, which is 662 00:37:55,989 --> 00:37:59,888 why it's actually more efficient and I'll give you a copy to give you the actual thing. 663 00:37:59,889 --> 00:38:02,798 Sometimes what I might want to do is say, "Hey you know what, 664 00:38:02,798 --> 00:38:05,179 I want to swap two values around." 665 00:38:05,179 --> 00:38:08,568 And so you might think, "Hey you know what, if I have some array, here's some integer array that 666 00:38:08,568 --> 00:38:12,219 I declare here that's got five elements in it. And I set its first two values to be one and 667 00:38:12,219 --> 00:38:16,748 two, respectively. So array sub zero is one, array sub one is two." Okay? 668 00:38:16,748 --> 00:38:21,259 Then I say, "Hey, I want to swap the two values." So I have some function that Mehran wrote 669 00:38:21,259 --> 00:38:26,639 for me, some method called Swap Elements Buggy. And why did he name it that? Yeah not because it's 670 00:38:26,639 --> 00:38:30,498 a little baby that's inside a little stroller that we push around, that in the days of yore we used to 671 00:38:30,498 --> 00:38:32,989 call a buggy, because it's got an error. 672 00:38:32,989 --> 00:38:36,399 This not the way you want to do it. And I do want to make it explicitly clear so this 673 00:38:36,400 --> 00:38:37,619 is the buggy version. 674 00:38:37,619 --> 00:38:43,259 The buggy version says, "I pass in these two elements of the arrays as the two parameters." Which 675 00:38:43,260 --> 00:38:46,189 means when I want to do swap, here's the buggy version, 676 00:38:46,188 --> 00:38:50,478 the buggy version says, "Hey, I get passed an integer X and an integer Y." Why do I get an integer X and an 677 00:38:50,478 --> 00:38:51,849 integer 678 00:38:51,849 --> 00:38:56,858 Y? Because the elements that I passed in were array sub zero and array sub one, those are integers. 679 00:38:56,858 --> 00:39:01,139 They are primitive values. I'm not passing the whole array; I'm passing individual elements 680 00:39:01,139 --> 00:39:04,269 of the array. The individual elements of the array 681 00:39:04,269 --> 00:39:09,230 are primitive types, which means we go back to our old case of passing copies. Anytime 682 00:39:09,230 --> 00:39:13,820 the things here are just straightforward ints, rather than being an integer array, for example, 683 00:39:13,820 --> 00:39:15,480 all you're getting are copies. 684 00:39:15,480 --> 00:39:18,440 So I get copies of array sub zero and array sub one, 685 00:39:18,440 --> 00:39:22,700 and I go through this little exercise where I swap them. So I have some temporary where I put 686 00:39:22,699 --> 00:39:23,539 one of them in, 687 00:39:23,539 --> 00:39:24,289 then I take 688 00:39:24,289 --> 00:39:27,690 that one and I replace it with the other one, and then I assign the temporary back to the other 689 00:39:27,690 --> 00:39:32,139 one. It's not a big deal how it actually works. As a matter of fact, let me just draw it for you, how it works so 690 00:39:32,139 --> 00:39:34,989 you know how it works and we're all on the same page. 691 00:39:34,989 --> 00:39:36,608 I get X and Y, 692 00:39:36,608 --> 00:39:39,460 they have values, let's say four and five to start with. 693 00:39:39,460 --> 00:39:43,940 I have some temporary. I start of by saying: temporary equals X, so temporary gets the value 694 00:39:43,940 --> 00:39:50,650 four, then X gets Y, so X gets the value five, then Y gets temp 695 00:39:50,650 --> 00:39:55,200 so Y gets the value four. And for all intents and purposes, it looks like I swapped the values. 696 00:39:55,199 --> 00:39:59,939 Good times, now I'm done with this function and this all goes away because they're copies 697 00:39:59,940 --> 00:40:03,720 and they're all temporary. And they just vanish and nothing is actually changed in the array 698 00:40:03,719 --> 00:40:07,949 because I passed the individual elements of the array, which are primitive types. 699 00:40:07,949 --> 00:40:11,929 So if you pass primitive types, even if they're individual elements of an array, you are still getting 700 00:40:11,929 --> 00:40:12,778 copies. 701 00:40:12,778 --> 00:40:17,378 If you really want to do the swap, the right way to do it, is Swap Elements Happy 702 00:40:17,378 --> 00:40:22,858 because it will make you happy. What you do is you say, "Hey, I'm gonna pass the whole array." 703 00:40:22,858 --> 00:40:26,608 Once you get the whole array, you've gotten a reference to the array, you can change stuff 704 00:40:26,608 --> 00:40:28,639 around in the array and it will persist. 705 00:40:28,639 --> 00:40:32,579 So you say, "Hey, I'm gonna give you the whole array, and I'm gonna give you the two positions 706 00:40:32,579 --> 00:40:35,148 of the elements in the array that I want to swap." 707 00:40:35,148 --> 00:40:39,398 So when I actually do the swap here I'm doing the same logic, except I'm actually doing the 708 00:40:39,398 --> 00:40:40,000 swap 709 00:40:40,000 --> 00:40:42,099 with values in the array 710 00:40:42,099 --> 00:40:45,360 rather than copies of values that are passed in as primitives. 711 00:40:45,360 --> 00:40:47,440 So this version actually works 712 00:40:47,440 --> 00:40:49,909 because I'm modifying the array itself. 713 00:40:49,909 --> 00:40:55,829 Okay, and questions about that? 714 00:40:55,829 --> 00:40:59,009 Uh huh? If there are objects [inaudible]? If they're objects are actually getting references to the object so you don't need 715 00:40:59,009 --> 00:41:02,199 to do the same thing. It's just for primitives that you need to worry about it. 716 00:41:02,199 --> 00:41:06,259 So let me just run this program just to show you, in its painful detail, what it's actually 717 00:41:06,259 --> 00:41:07,338 doing. 718 00:41:07,338 --> 00:41:09,088 We're 719 00:41:09,088 --> 00:41:09,920 running. 720 00:41:09,920 --> 00:41:14,068 We're running. Oh, it's ? sometimes it's just slow and painful, 721 00:41:14,068 --> 00:41:18,228 but at the end of the day, you know, we get the little warm fuzzy. So I want to do the Swap 722 00:41:18,228 --> 00:41:19,248 Example. 723 00:41:19,248 --> 00:41:23,928 And so the Buggy Swap, notice Element 1 is still ? or Element 0 is still one, Element 1 724 00:41:23,929 --> 00:41:28,209 is still two. Happy Swap, they've actually swapped in place because I passed the array, which 725 00:41:28,208 --> 00:41:30,838 was a reference. Okay, 726 00:41:30,838 --> 00:41:33,739 so any questions about that? If that 727 00:41:33,739 --> 00:41:36,449 all made sense, nod your head. 728 00:41:36,449 --> 00:41:38,769 Exellente. All right, so a 729 00:41:38,768 --> 00:41:43,199 couple more quick things. One quick thing, this is super-minor. You'll probably never 730 00:41:43,199 --> 00:41:46,489 end up doing this in your life, but if you see someone else do it you'll know what they're 731 00:41:46,489 --> 00:41:51,269 doing. Which is you can create an array that's initialized with particular values you give it 732 00:41:51,269 --> 00:41:53,929 by saying int bracket 733 00:41:53,929 --> 00:41:57,049 array, so we have some array of integers. And 734 00:41:57,050 --> 00:42:01,630 rather than specifying new or specifying some size, I just say, "Yeah, this array actually has the 735 00:42:01,630 --> 00:42:02,450 values 736 00:42:02,449 --> 00:42:05,408 two, four, six, and eight in it." 737 00:42:05,409 --> 00:42:10,059 So inside braces I put the initial values, from the number of values that I give it, the values are 738 00:42:10,059 --> 00:42:11,300 separated by commas, 739 00:42:11,300 --> 00:42:16,089 it figures out, "Oh, this is an array of four elements, so let me create an array of four elements 740 00:42:16,088 --> 00:42:20,489 for you, stick in those initial values to initialize it, and you're good to go from there." You virtually 741 00:42:20,489 --> 00:42:21,868 never see this, 742 00:42:21,869 --> 00:42:25,880 but just in case you do, now you know what it's like. All right? 743 00:42:25,880 --> 00:42:26,989 So 744 00:42:26,989 --> 00:42:31,059 it's time for one last great topic. And this last great topic, I'm gonna give you very 745 00:42:31,059 --> 00:42:32,180 briefly 746 00:42:32,179 --> 00:42:36,239 just enough for right now that you need to know to do your hangman program. 747 00:42:36,239 --> 00:42:40,708 On Wednesday, we'll through all this is excruciating detail. So if you're not starting hangman before 748 00:42:40,708 --> 00:42:41,928 Wednesday, 749 00:42:41,929 --> 00:42:45,869 you're perfectly fine. You'll see way more detail than you need for hangman on Wednesday. But 750 00:42:45,869 --> 00:42:49,689 just in case you were like, "Hey Mehran, yeah I'm studying for the midterm, and right after the midterm 751 00:42:49,688 --> 00:42:54,298 I'm totally pumped up. I'm just gonna go do all of hangman," this is what you need for Part 752 00:42:54,298 --> 00:42:57,608 3 of hangman. So Part 1 and 2 you already know everything you need to know. 753 00:42:57,608 --> 00:43:01,549 Part 3, there's this thing called the array list. How many people have already gotten to Part 3 of hangman, 754 00:43:01,550 --> 00:43:02,419 by the way? 755 00:43:02,418 --> 00:43:07,888 Yeah, just one person in the back; did you read about array list yourself or have you not done it yet? [Inaudible]. 756 00:43:07,889 --> 00:43:11,130 I can't hear you but that's okay. If you've 757 00:43:11,130 --> 00:43:14,949 already gotten to Part 3 you're so far ahead you just don't even need to worry. It's good 758 00:43:14,949 --> 00:43:19,459 times. Just come after class, I'll give you all the chocolate you want. All right. So 759 00:43:19,458 --> 00:43:22,648 array list. 760 00:43:22,648 --> 00:43:26,388 Because these array things are sometimes bulky to work with, especially when you think about, 761 00:43:26,389 --> 00:43:31,159 "Oh, I need this effective size versus actual size, and wouldn't it be great if when I needed 762 00:43:31,159 --> 00:43:35,869 more elements the array just grew for me?" So I said, "Hey, I start off with no elements and I read in 763 00:43:35,869 --> 00:43:40,749 like five values for midterm scores and I kinda store that." And I say, "That's great." Oh, I got these ? 764 00:43:40,748 --> 00:43:45,278 I found these like late midterms somewhere, well there's five more I need to add. And the 765 00:43:45,278 --> 00:43:48,998 array would just come along and say, "Oh that's fine. You've been so 766 00:43:48,998 --> 00:43:50,419 good to me this whole time, I'm just 767 00:43:50,420 --> 00:43:53,240 gonna expand my size to give you more space." Like 768 00:43:53,239 --> 00:43:54,928 wouldn't that be great? 769 00:43:54,929 --> 00:43:59,048 And you're like, "Oh yeah, that'd be so good." Like the birds would be singing and everything. But 770 00:43:59,048 --> 00:44:01,108 real arrays don't do that. 771 00:44:01,108 --> 00:44:04,358 Yeah, sorry, birds all been shot down. 772 00:44:04,358 --> 00:44:09,179 Array lists bring the birds back to life and now they're singing again. It will do this for you, 773 00:44:09,179 --> 00:44:12,939 so what you need to know about an array list. First of all, what you need to do is you need 774 00:44:12,938 --> 00:44:19,208 to import java-dot-util-dot-star because this thing called an array list comes from the 775 00:44:19,208 --> 00:44:21,759 Java util package. Okay? 776 00:44:21,759 --> 00:44:23,559 Once you do that, 777 00:44:23,559 --> 00:44:27,890 you get access to our friend the array list. And the array list as described in the book comes 778 00:44:27,889 --> 00:44:29,150 in two flavors. 779 00:44:29,150 --> 00:44:34,760 It comes in the super-cool Java 5.0, also known as Java 1.5. 780 00:44:34,760 --> 00:44:36,949 Because sometimes the numbers for Java 781 00:44:36,949 --> 00:44:41,739 is 1-point-something, like 1.x and sometimes it's just X; and it's super confusing 782 00:44:41,739 --> 00:44:44,179 but that's the way it is. So Java 1.5 is the same thing 783 00:44:44,179 --> 00:44:49,000 is the same thing as Java 5. And you're like, "That makes no sense at all." Yeah, it doesn't. That's 784 00:44:49,000 --> 00:44:50,179 just the way it is. 785 00:44:50,179 --> 00:44:54,259 So there's the super-cool Java 5.0 or later version, and then there's 786 00:44:54,259 --> 00:44:58,949 yes still kind of useful but not quite as cool pre-Java 5.0 version. 787 00:44:58,949 --> 00:45:03,469 And the only thing you need to know is the super-cool Java 5.0 or later version, so 788 00:45:03,469 --> 00:45:06,788 you don't need to worry about the pre-5.0 version. So 789 00:45:06,789 --> 00:45:08,220 in the super-cool 790 00:45:08,219 --> 00:45:10,278 5.0 or later version, 791 00:45:10,278 --> 00:45:13,489 an array list is something called a template, 792 00:45:13,489 --> 00:45:17,989 which means when I'm creating an array list, an array list is actually a class 793 00:45:17,989 --> 00:45:22,900 that I specify the type that that class is gonna operate on, which is kind of a funky thing. 794 00:45:22,900 --> 00:45:25,039 But the way I declare an array list 795 00:45:25,039 --> 00:45:26,989 is I say: array list 796 00:45:26,989 --> 00:45:32,548 with AL because this is actually a class that exists inside Java-util-dot-star. 797 00:45:32,548 --> 00:45:36,708 And then I use essentially the less-than sign or what we sometimes refer to as angle 798 00:45:36,708 --> 00:45:41,068 brackets if we're not using it as a less-than sign. And then the type that I wanna 799 00:45:41,068 --> 00:45:45,648 store in this array list. So essentially, I wanna have an array list of strings. And you 800 00:45:45,648 --> 00:45:49,128 can kind of think of this almost like an array of strings but cooler. 801 00:45:49,128 --> 00:45:52,750 And so, this is inside the less-than/greater-than signs. Okay? 802 00:45:52,750 --> 00:45:56,858 Then you give it the name; I might call this STR List, for string list. 803 00:45:56,858 --> 00:45:59,708 And then I say that is a new, 804 00:45:59,708 --> 00:46:03,978 and here's where the syntax gets kind of very funky, array list 805 00:46:03,978 --> 00:46:05,468 angle bracket 806 00:46:05,469 --> 00:46:10,179 string close angle bracket, open paren close paren. 807 00:46:10,179 --> 00:46:13,088 And you look at that and you're like, "What? 808 00:46:13,088 --> 00:46:17,268 Why would I do that?" That's just the way the syntax is. What this is saying is I'm gonna 809 00:46:17,268 --> 00:46:18,469 have an array list. 810 00:46:18,469 --> 00:46:21,679 What am I gonna store in the array list? I'm gonna store strings. 811 00:46:21,679 --> 00:46:25,728 What's the name of my variable that is that array list? 812 00:46:25,728 --> 00:46:28,779 It is STR List, so that's the name of the variable. 813 00:46:28,780 --> 00:46:32,879 The type is an array list of strings. Okay? 814 00:46:32,878 --> 00:46:38,528 Once I set this up, all I get is my little space for a STR List 815 00:46:38,528 --> 00:46:42,559 and so I need to actually say, "Hey, create the new array list for me." 816 00:46:42,559 --> 00:46:48,330 What kind of array list do I want? I need an array list that stores strings. I'm calling a constructor 817 00:46:48,329 --> 00:46:54,150 that takes no parameters. So I still need to specify an argument list for that constructor, 818 00:46:54,150 --> 00:46:56,840 which is where the open paren/close paren comes from. 819 00:46:56,840 --> 00:47:00,410 So the name of the constructor, the way you can think of it, is actually array list 820 00:47:00,409 --> 00:47:03,480 with the type specified, as funky as that may seem, 821 00:47:03,480 --> 00:47:05,130 and then the parameters 822 00:47:05,130 --> 00:47:07,289 are nothing. Okay? 823 00:47:07,289 --> 00:47:11,260 And so what this now gives me is STR List point somewhere in memory 824 00:47:11,260 --> 00:47:12,599 that stores my 825 00:47:12,599 --> 00:47:16,588 array list, which is some object that's gonna store strings in it. Okay? 826 00:47:16,588 --> 00:47:18,659 How do I use that? 827 00:47:18,659 --> 00:47:21,548 Let me show you a real quick example. 828 00:47:21,548 --> 00:47:25,548 So because an array list is an object, it has methods on it. Okay? 829 00:47:25,548 --> 00:47:30,079 One of the methods I can do is I can say STR List 830 00:47:30,079 --> 00:47:31,809 dot add. 831 00:47:31,809 --> 00:47:37,298 And what add does, I would give it some string. So I might have some string line that 832 00:47:37,298 --> 00:47:40,778 is Hello, 833 00:47:40,778 --> 00:47:46,909 and I add line. What the add method does is it adds that element to the end of the array 834 00:47:46,909 --> 00:47:49,529 and grows the size of the array by one. 835 00:47:49,530 --> 00:47:53,580 So if I want to add a whole bunch of things to some STR List, I start off by creating 836 00:47:53,579 --> 00:47:56,759 a STR List, which is empty. So when I do this I get an array list, 837 00:47:56,759 --> 00:48:00,559 which is empty called STR List. And as soon as I do this add 838 00:48:00,559 --> 00:48:05,030 it grows to size one, and it now has the line Hello as the first element. 839 00:48:05,030 --> 00:48:06,778 If I were to do another add, 840 00:48:06,778 --> 00:48:09,369 like say STR List 841 00:48:09,369 --> 00:48:14,489 dot add, and maybe I say There as another string 842 00:48:14,489 --> 00:48:18,358 and now it grows to size two and There is the second element. So add 843 00:48:18,358 --> 00:48:20,259 adds it, appends to the end, 844 00:48:20,260 --> 00:48:22,309 and grows the size by one. 845 00:48:22,309 --> 00:48:26,530 Some other things I can do with STR Lists. I can ask STR Lists, or array lists in general, 846 00:48:26,530 --> 00:48:31,879 for their size. So there's a method called size open paren close paren. 847 00:48:31,878 --> 00:48:36,239 What this does is it gives you back as an integer how many elements are inside your 848 00:48:36,239 --> 00:48:37,179 STR List 849 00:48:37,179 --> 00:48:39,739 based on how many you've added. Okay? 850 00:48:39,739 --> 00:48:43,039 Another thing you can also do is you can say, "Well, it's great to be able to add all these 851 00:48:43,039 --> 00:48:48,489 elements, Mehran, but how do I actually get information out of my array of STR Lists?" What you say 852 00:48:48,489 --> 00:48:54,949 is: STR List dot get. So there is like getter, much like there's a setter, and you pass it in an 853 00:48:54,949 --> 00:48:56,220 index, I. 854 00:48:56,219 --> 00:48:59,250 And so, what this says is get the element at the I 855 00:48:59,250 --> 00:49:04,009 position. So if I've added line here, which is Hello, that's a position zero. 856 00:49:04,009 --> 00:49:08,349 When I add There that's a position one. You can think of it an array that's just dynamically growing 857 00:49:08,349 --> 00:49:10,360 as I add more stuff to the end. 858 00:49:10,360 --> 00:49:14,940 And the way I refer to the individual elements is with get and I still specify an index 859 00:49:14,940 --> 00:49:17,389 that starts from zero. Okay? And 860 00:49:17,389 --> 00:49:21,329 this is pretty much all you need to know to be able to do Part 3 of hangman. And you'll 861 00:49:21,329 --> 00:49:24,829 see it in much more excruciating detail on Wednesday, but 862 00:49:24,829 --> 00:49:26,449 that's all you need for now. 863 00:49:26,449 --> 00:49:29,749 Any questions about array lists? 864 00:49:29,748 --> 00:49:32,389 All righty, then good luck on the midterm, I'll see you tomorrow night.