1 00:00:08,769 --> 00:00:11,028 2 00:00:11,028 --> 00:00:14,298 This presentation is delivered by the Stanford Center for Professional 3 00:00:14,298 --> 00:00:21,298 Development. 4 00:00:24,739 --> 00:00:28,750 What? Sir Lancelot. Not Lancelot. [Inaudible] I 5 00:00:28,750 --> 00:00:33,079 almost considered Karel. I 6 00:00:33,079 --> 00:00:37,009 almost went for Karel. I got the big box, but it's kind of hard to draw, sort of like, Karel 7 00:00:37,009 --> 00:00:41,619 cannot draw that much in the big box. No. 8 00:00:41,619 --> 00:00:45,250 Well, I'll give you hints as we kind of go along, and at the very end I'll 9 00:00:45,250 --> 00:00:47,649 show you if you can't figure it out. I almost considered 10 00:00:47,649 --> 00:00:51,170 giving whoever could figure it out a 100 on the midterm, but I figured 11 00:00:51,170 --> 00:00:54,940 that was kind of just a [inaudible] way of giving you a 100 on the midterm, so 12 00:00:54,939 --> 00:00:56,809 I'll just give you lots of candy. 13 00:00:56,810 --> 00:01:00,080 So anyway, just think about it, and if you want to try to search the 14 00:01:00,079 --> 00:01:03,210 web or whatever, it is a one particular character, 15 00:01:03,210 --> 00:01:06,129 and it's not a wizard. I actually thought, "Oh, I could just make it easy for 16 00:01:06,129 --> 00:01:07,859 you and just come in and be, like, oh, 17 00:01:07,859 --> 00:01:10,140 ha, ha," because most people, 18 00:01:10,140 --> 00:01:15,728 without the hat, would see it and they'd go, "Oh, you're a wizard, right?" No. So I 19 00:01:15,728 --> 00:01:17,739 just brought the hat to kind of fool you. 20 00:01:17,739 --> 00:01:19,890 All right. So with that said, 21 00:01:19,890 --> 00:01:23,960 time to actually wrap up one of our last topics, which was arrays. We'll look at a 22 00:01:23,959 --> 00:01:27,159 few more advanced things we can do with the arrays and then get into the details of our 23 00:01:27,159 --> 00:01:31,549 next great topic, which is our friend the ArrayList. So 24 00:01:31,549 --> 00:01:35,509 when we talked about arrays, a couple people last time said, "Hey, Miron, can I have 25 00:01:35,510 --> 00:01:36,450 26 00:01:36,450 --> 00:01:39,700 multi-dimensional arrays, can I have arrays that have more than one index, and 27 00:01:39,700 --> 00:01:41,590 in fact, you can. 28 00:01:41,590 --> 00:01:44,618 We do refer to these things as multi-dimensional arrays. And so one way 29 00:01:44,618 --> 00:01:47,079 you might want to do it is let's say you wanted to have 30 00:01:47,079 --> 00:01:51,269 this dreaded thing called a matrix. How many people know what a matrix is? 31 00:01:51,269 --> 00:01:54,739 I'm not talking about the bad movie. Actually, it was a good movie. The sequels, not 32 00:01:54,739 --> 00:01:56,049 so great. 33 00:01:56,049 --> 00:01:59,849 It's basically just a grid of information. It's a grid mostly of 34 00:01:59,849 --> 00:02:02,449 numbers if you want to think about it in a mathematical sense, but really 35 00:02:02,450 --> 00:02:06,228 we think of this thing as a two-dimensional array. 36 00:02:06,228 --> 00:02:09,949 And so the way you can think about is if I have a matrix that's a [2] [3] 37 00:02:09,949 --> 00:02:13,328 matrix what that means is it's a grid that 38 00:02:13,329 --> 00:02:14,830 has two rows 39 00:02:14,830 --> 00:02:19,250 and three columns. Okay. And you might look at that and say, "Hey, that almost likes an array, it 40 00:02:19,250 --> 00:02:21,509 looks like two arrays stacked on top of each other," 41 00:02:21,509 --> 00:02:23,318 and in fact, you would be correct. 42 00:02:23,318 --> 00:02:26,978 If we have multi-dimensional arrays, what we're actually creating our arrays of 43 00:02:26,979 --> 00:02:27,770 arrays. 44 00:02:27,770 --> 00:02:30,810 So really what we want to do here, if we want to create this thing called a 45 00:02:30,810 --> 00:02:31,650 matrix, 46 00:02:31,650 --> 00:02:35,530 is we specify the type that we're gonna store in all of the boxes in that 47 00:02:35,530 --> 00:02:36,139 grid, 48 00:02:36,139 --> 00:02:37,600 and then we specify 49 00:02:37,599 --> 00:02:42,549 as many pairs of square brackets as we have dimensions of that thing. So if 50 00:02:42,550 --> 00:02:44,230 we have two dimensions 51 00:02:44,229 --> 00:02:48,268 here like we have rows and columns then we specify two open and closed brackets in 52 00:02:48,269 --> 00:02:51,260 a row, if we had three like we wanted to make a little thing that represented a 53 00:02:51,259 --> 00:02:52,289 cube 54 00:02:52,289 --> 00:02:55,908 we'd have three sets of brackets. So here I'm just having two because I'm 55 00:02:55,908 --> 00:02:57,719 just gonna show you two arrays so you can 56 00:02:57,719 --> 00:03:00,058 think about the generalization from there. And 57 00:03:00,058 --> 00:03:03,219 then we give it a name so we might call this thing a matrix. 58 00:03:03,219 --> 00:03:03,919 And then 59 00:03:03,919 --> 00:03:06,298 we specify 60 00:03:06,299 --> 00:03:10,429 basically the creation of that matrix in the same way we created an array except 61 00:03:10,429 --> 00:03:14,908 now we have more than one dimension, so here we say new, we specify the type 62 00:03:14,908 --> 00:03:16,158 again, like, indices 63 00:03:16,158 --> 00:03:20,478 and then to give it the size rather than just having one size that we specify, 64 00:03:20,479 --> 00:03:24,079 we have a size for every dimension. So if this things gonna be [2] [3], 65 00:03:24,079 --> 00:03:26,359 we specify the first dimension as two 66 00:03:26,359 --> 00:03:28,699 and then the second dimension as three. 67 00:03:28,699 --> 00:03:32,499 And each one of these is its own pair of square brackets. We don't use comma notations, so if 68 00:03:32,498 --> 00:03:34,949 you've worked with some other language or you say 69 00:03:34,949 --> 00:03:36,299 2, 3, nuh uh. 70 00:03:36,300 --> 00:03:38,329 It doesn't work in java. 71 00:03:38,329 --> 00:03:42,409 It's [2] [3] no spaces immediately after it. 72 00:03:42,408 --> 00:03:45,298 And so what this does when you create that is it sort of creates this thing 73 00:03:45,299 --> 00:03:48,229 that's this grid that's a [2] [3] grid. 74 00:03:48,229 --> 00:03:51,059 And now you can refer to the individual elements of that grid, so the way you can 75 00:03:51,058 --> 00:03:53,068 think about it is 76 00:03:53,068 --> 00:03:55,538 this is element [0] [0] or 77 00:03:55,538 --> 00:03:58,268 really you want to think of them in brackets, 78 00:03:58,269 --> 00:04:00,908 and then this becomes element [0] 79 00:04:00,908 --> 00:04:06,718 [1] and this is element [0] [2] and so forth, so this is element [1] 80 00:04:06,718 --> 00:04:09,038 [0] and this is [1] 81 00:04:09,038 --> 00:04:13,058 [1] and this is [1] [2]. 82 00:04:13,058 --> 00:04:16,168 So if you kind of think about assigning to elements of that you could say something 83 00:04:16,168 --> 00:04:18,968 like matrix 84 00:04:18,968 --> 00:04:20,379 [0] 85 00:04:20,379 --> 00:04:25,229 [1] = 5 and what that will do is it'll say, "Oh, where is [0] [1], here is [0] [1]." 86 00:04:25,228 --> 00:04:28,798 Well, what I'm gonna do is, in that particular cell in your grid, is 87 00:04:28,798 --> 00:04:31,519 just gonna stick in the value five. 88 00:04:31,519 --> 00:04:34,908 And you can use that like an integer anywhere else you could use it like an integer or a 89 00:04:34,908 --> 00:04:37,180 single element of an array. 90 00:04:37,180 --> 00:04:39,000 Okay. The other thing that's kind of funky about this 91 00:04:39,000 --> 00:04:42,519 is that this two-dimensional array is really - the way you can think of it is 92 00:04:42,519 --> 00:04:44,709 it's an array of arrays, 93 00:04:44,709 --> 00:04:46,498 right? It's an array - 94 00:04:46,499 --> 00:04:47,240 think of it 95 00:04:47,240 --> 00:04:51,329 this way as an array. It has two entries in it and what is each one of 96 00:04:51,329 --> 00:04:54,500 those entries of the array, well, really every entry of the array you can think of 97 00:04:54,500 --> 00:04:56,689 this as one cell, that whole thing, 98 00:04:56,689 --> 00:05:00,370 and this whole thing as another cell and each entry of that first dimension of 99 00:05:00,370 --> 00:05:01,259 the array 100 00:05:01,259 --> 00:05:02,920 is just another array 101 00:05:02,920 --> 00:05:06,039 with three elements in it. Okay? So 102 00:05:06,038 --> 00:05:10,849 what that allows you to do is say something like matrix subzero 103 00:05:10,850 --> 00:05:12,930 and matrix subzero, the type 104 00:05:12,930 --> 00:05:14,090 of this thing, 105 00:05:14,089 --> 00:05:15,469 is an integer array. 106 00:05:15,470 --> 00:05:19,939 A single dimensional array because what is matrix subzero, it's 107 00:05:19,939 --> 00:05:22,400 this full line here. 108 00:05:22,399 --> 00:05:25,439 It's the first row of the entire matrix, 109 00:05:25,439 --> 00:05:29,149 and then if I specify something else, like I specify a column, 110 00:05:29,149 --> 00:05:32,779 that's how I get [0] [1]. First I specify the row, and then I specify the 111 00:05:32,779 --> 00:05:33,279 column. 112 00:05:33,279 --> 00:05:36,279 If I don't specify a column this is referring to the whole row, which means 113 00:05:36,279 --> 00:05:40,538 it's just referring to a whole array. Can 114 00:05:40,538 --> 00:05:43,839 you refer to [inaudible] You can't. You have to refer to things in order, so there's no way to 115 00:05:43,839 --> 00:05:46,129 just pull out a column, so if you're feeling sort of like, 116 00:05:46,129 --> 00:05:51,319 "Oh, but in my Math 53 class we do all this stuff with columns." Yeah. 117 00:05:51,319 --> 00:05:55,430 You just have to pick, in some sense, what's gonna be your major ordering for 118 00:05:55,430 --> 00:05:57,250 this thing and have that one be first. 119 00:05:57,250 --> 00:05:59,939 So you could kind of think of it in your head that, "Oh, well, my first index is 120 00:05:59,939 --> 00:06:02,750 really my columns and think of this thing as being the transposed and all 121 00:06:02,750 --> 00:06:03,069 that," 122 00:06:03,069 --> 00:06:06,049 but you just have to keep track of it in your head. Okay? 123 00:06:06,048 --> 00:06:08,388 Any questions about that? 124 00:06:08,389 --> 00:06:11,519 All right. So we can write a little code that makes use of this 125 00:06:11,519 --> 00:06:13,459 just so you see a quickie example. 126 00:06:13,459 --> 00:06:17,169 And we could, for example, go through all the elements of this grid and set our 127 00:06:17,170 --> 00:06:18,550 initial values to be one, 128 00:06:18,550 --> 00:06:23,009 so we could have something like I = 0. "I" is less than - what you want 129 00:06:23,009 --> 00:06:24,189 "I" to be less than 130 00:06:24,189 --> 00:06:28,560 in the first index is 2. I ++ and then you're gonna have some 131 00:06:28,560 --> 00:06:32,910 other four loop. Let's say this is J = 0, J is less 132 00:06:32,910 --> 00:06:37,789 than 3 because that's the size of my second index, J++. 133 00:06:37,788 --> 00:06:43,129 And then inside here I could just say something like matrix I 134 00:06:43,129 --> 00:06:46,240 sub I, sub J = 1. 135 00:06:46,240 --> 00:06:51,280 And that would initialize all the cells in my grid to be one. Okay? Is there 136 00:06:51,279 --> 00:06:54,129 any questions about that? Hopefully fairly straightforward kind of stuff. 137 00:06:54,129 --> 00:06:57,740 You can have as many dimensions of arrays as you want sort of within reason, and 138 00:06:57,740 --> 00:07:01,019 within reason being how much memory your computer has right because if you imagine, 139 00:07:01,019 --> 00:07:05,168 "Hey, I don't only want a [2] [3] matrix, I want 140 00:07:05,168 --> 00:07:09,240 a [2] [3] bi-hundred matrix," so this becomes a three dimensional array. 141 00:07:09,240 --> 00:07:13,660 Right? So I basically have this grid repeated a hundred times going forward. 142 00:07:13,660 --> 00:07:16,349 Suddenly, you're using up a whole bunch of memory. Right? 143 00:07:16,348 --> 00:07:19,348 So you want to be careful doing that, but if you wanted to do that you would just add 144 00:07:19,348 --> 00:07:21,349 another pair of brackets here 145 00:07:21,350 --> 00:07:24,670 and you'd add another pair of brackets over here with a hundred and now you would 146 00:07:24,670 --> 00:07:28,009 have a three dimensional array. So if you want to create, like, your friend the Rubik's 147 00:07:28,009 --> 00:07:30,300 Cube or something, with is [3] [3] [3], 148 00:07:30,300 --> 00:07:32,639 now you know how. Okay? 149 00:07:32,639 --> 00:07:34,810 And if you want to do real funky things with 150 00:07:34,810 --> 00:07:37,149 four and five dimensional objects or whatever, 151 00:07:37,149 --> 00:07:39,269 be my guest. Okay? 152 00:07:39,269 --> 00:07:41,860 But we're not gonna use them a whole bunch in this class, so I might show you some examples 153 00:07:41,860 --> 00:07:45,968 of two "D" stuff momentarily, but most of the arrays we're gonna deal with are just 154 00:07:45,968 --> 00:07:49,509 what we refer to as one dimensional arrays, right, which is just a list. 155 00:07:49,509 --> 00:07:51,949 All right? So any questions about multi-dimensional arrays? 156 00:07:51,949 --> 00:07:55,119 If you're feeling okay with multi-dimensional arrays nod your head. 157 00:07:55,119 --> 00:07:57,619 Good times. All right. So 158 00:07:57,619 --> 00:07:58,919 moving on from the array 159 00:07:58,918 --> 00:08:02,529 we can now go back to our friend that I briefly introduced last time, and now we 160 00:08:02,529 --> 00:08:04,438 can go into 161 00:08:04,439 --> 00:08:05,419 excruciating detail, 162 00:08:05,418 --> 00:08:07,748 our friend the ArrayList. Okay? 163 00:08:07,749 --> 00:08:12,149 And the ArrayList - the way you can think about is it's an array just kind of 164 00:08:12,149 --> 00:08:13,819 cooler. Okay? 165 00:08:13,819 --> 00:08:18,239 So the idea behind an ArrayList is an ArrayList first of all really is 166 00:08:18,238 --> 00:08:22,828 an object, and it's an object that's provided for you in the Java Util 167 00:08:22,829 --> 00:08:25,889 Package. We talked a little about this last time. I'm just repeating it just for 168 00:08:25,889 --> 00:08:26,629 good measure. 169 00:08:26,629 --> 00:08:30,860 So you import the Java Util that's star packet to be able to get 170 00:08:30,860 --> 00:08:33,009 at the ArrayList class. Okay? 171 00:08:33,009 --> 00:08:36,688 And the way you can think about an ArrayList is it's really like an array, 172 00:08:36,688 --> 00:08:40,578 except it's an array that dynamically changes its size depending on how you 173 00:08:40,578 --> 00:08:42,309 add elements to it. So 174 00:08:42,309 --> 00:08:43,208 you can just say, "Hey, 175 00:08:43,208 --> 00:08:47,739 add another element to the end," and it will just go, "Hey, you know what, I'll just create extra elements 176 00:08:47,740 --> 00:08:51,240 at the end for you and now your size is one greater." 177 00:08:51,240 --> 00:08:54,659 And so the way to think about it is in terms of effective and actual size, that 178 00:08:54,659 --> 00:08:57,829 we talked about last time, was with regular arrays you set up with the actual sizes 179 00:08:57,830 --> 00:08:58,759 that's declared, and then 180 00:08:58,759 --> 00:09:01,159 your effective size is how much of it that you're using. 181 00:09:01,159 --> 00:09:05,019 With an ArrayList, think of it as the effective size and the actual size, at 182 00:09:05,019 --> 00:09:06,589 least stylistically, 183 00:09:06,589 --> 00:09:08,370 are exactly the same thing. 184 00:09:08,370 --> 00:09:12,029 Whenever you want to add another element to the end, its effective and actual 185 00:09:12,029 --> 00:09:13,789 size both grow. Okay? 186 00:09:13,789 --> 00:09:16,689 Now underneath the hood, the implementation actually involves some 187 00:09:16,690 --> 00:09:20,390 stuff where the effective and actual size can be different, but that's all abstracted way 188 00:09:20,389 --> 00:09:22,240 for you. As far as you're concerned, 189 00:09:22,240 --> 00:09:25,539 effective and actual sizes are basically the same thing. 190 00:09:25,539 --> 00:09:30,240 Okay? And it provides some other functionality that is nice, and I will show you that in just a moment. But 191 00:09:30,240 --> 00:09:33,350 this particular ArrayList thing is called a template. 192 00:09:33,350 --> 00:09:36,200 I'll give you a little history of templates. I mentioned 193 00:09:36,200 --> 00:09:39,320 this briefly last time, but I will mention it again. 194 00:09:39,320 --> 00:09:40,660 Starting in Java, 195 00:09:40,659 --> 00:09:44,209 the book refers to it as Java 5.0. A lot of programmers will say 196 00:09:44,210 --> 00:09:47,769 Java 5.0. Some programmers will say Java 1.5, 197 00:09:47,769 --> 00:09:51,038 and what you have installed on your computer is either Java 1.5 or Java 198 00:09:51,038 --> 00:09:54,749 1.6, which some programmers would say is either Java 5 or Java 199 00:09:54,749 --> 00:09:58,259 6, which you would look at and say, "Hey, man, that makes no sense at all, 200 00:09:58,259 --> 00:09:59,470 why is it like that?" 201 00:09:59,470 --> 00:10:02,200 So I'll give you a little bit of history. Quick history lesson for the day. 202 00:10:02,200 --> 00:10:05,190 Many years ago, oh about 1995, there was this thing called Java 203 00:10:05,190 --> 00:10:08,900 1.0. It came out, people were happy and went, "Oh, nice," and 204 00:10:08,899 --> 00:10:13,028 then Java 1.1 came out and people looked at that and said, "Yeah, nice," 205 00:10:13,028 --> 00:10:16,659 and then Java 1.2 came out and people said, 206 00:10:16,659 --> 00:10:17,399 "Nice!." 207 00:10:17,399 --> 00:10:19,439 So when they said, 208 00:10:19,440 --> 00:10:20,330 "Nice!," 209 00:10:20,330 --> 00:10:24,570 1.2 in some people's mind was such a major change in the language 210 00:10:24,570 --> 00:10:27,990 Java that they started calling it 2.0, 211 00:10:27,990 --> 00:10:31,440 and at that point when 1.3 comes out you can't say, 212 00:10:31,440 --> 00:10:35,000 "Oh, well, that's 2.1," right, because then it gets really confusing so then 213 00:10:35,000 --> 00:10:38,798 when 1.3 came out people called that Java 3.0. 214 00:10:38,798 --> 00:10:41,059 And 1.4 was 215 00:10:41,059 --> 00:10:44,529 4.0, 1.5 became 5.0, 1.6 became 6.0, but in 216 00:10:44,529 --> 00:10:48,069 fact, the confusion still exists so if you go and look in your system most of you on 217 00:10:48,070 --> 00:10:52,028 your system it will actually have this 1.X number. 218 00:10:52,028 --> 00:10:55,720 Most programmers will actually refer to it by just the X as opposed to the 219 00:10:55,720 --> 00:11:00,090 1.X. Don't be confused. They're really referring to the same thing. Okay? So 220 00:11:00,090 --> 00:11:04,160 what we're dealing with here, in terms of ArrayLists as templates, is Java 221 00:11:04,159 --> 00:11:08,000 5.0 or later so sometimes that's just written as 5.0 plus, which all of you 222 00:11:08,000 --> 00:11:11,100 should have because that's what you installed in the beginning of class. 223 00:11:11,100 --> 00:11:15,350 The book actually shows both the pre-5.0 and the post 5.0 224 00:11:15,350 --> 00:11:17,860 version. We're just gonna deal with the post 5.0 because at this 225 00:11:17,860 --> 00:11:18,568 point, 226 00:11:18,568 --> 00:11:21,719 the older version is just not worth worrying about. Okay? 227 00:11:21,720 --> 00:11:25,750 So what is this thing called a template that exists in Java 228 00:11:25,750 --> 00:11:28,590 5.0 and later versions? Okay. 229 00:11:28,590 --> 00:11:30,470 What a template is 230 00:11:30,470 --> 00:11:32,100 is it's basically a 231 00:11:32,100 --> 00:11:32,720 class 232 00:11:32,720 --> 00:11:36,190 that allows you to specialize it based on the type. 233 00:11:36,190 --> 00:11:37,290 What does that mean? 234 00:11:37,289 --> 00:11:41,089 Okay. So let me give you a few examples of what that actually means. 235 00:11:41,090 --> 00:11:45,050 What that means is when you see methods for this ArrayList class, and you'll see them 236 00:11:45,049 --> 00:11:47,629 listed in the book, you'll see something that looks like this. 237 00:11:47,629 --> 00:11:49,328 There's a method called Add 238 00:11:49,328 --> 00:11:52,979 and it returns a bullion and you're like, "Oh, that's all good and well," what kind of 239 00:11:52,980 --> 00:11:56,839 parameter does Add take and you'll see the spunky thing that looks like this, 240 00:11:56,839 --> 00:11:59,520 [T] element, 241 00:11:59,519 --> 00:12:01,870 and you look at that and say, "Oh, Miron, 242 00:12:01,870 --> 00:12:06,529 you never told me about the [T] type, what type is that?" 243 00:12:06,529 --> 00:12:10,819 And the way you can think about it is this is a parameterized type. 244 00:12:10,820 --> 00:12:14,950 When you create an ArrayList you say, "I'm gonna create an ArrayList of a 245 00:12:14,950 --> 00:12:19,560 particular type." So when you set up your ArrayList you 246 00:12:19,559 --> 00:12:23,369 create an ArrayList object. And when you create that object, as you saw last time 247 00:12:23,370 --> 00:12:27,019 very briefly, in angle brackets, which are actually less than and great than 248 00:12:27,019 --> 00:12:27,428 signs, 249 00:12:27,428 --> 00:12:29,250 you specify a type 250 00:12:29,250 --> 00:12:30,929 like string. So you 251 00:12:30,929 --> 00:12:34,879 say ArrayList string and we call this thing, for example, SList and we might 252 00:12:34,879 --> 00:12:37,129 create that to be a new 253 00:12:37,129 --> 00:12:38,950 ArrayList. 254 00:12:38,950 --> 00:12:44,970 Again, we specify the type string () because we're actually calling 255 00:12:44,970 --> 00:12:47,700 the constructor which takes no arguments. Okay? 256 00:12:47,700 --> 00:12:49,840 So you create this new ArrayList 257 00:12:49,840 --> 00:12:53,829 and this thing that you specify here, the type that you specify there, 258 00:12:53,828 --> 00:12:56,949 is what we refer to as the type or the template. 259 00:12:56,950 --> 00:12:59,859 You can think of the template as having all these holes in it, 260 00:12:59,859 --> 00:13:03,278 and the way those holes get filled in is they get filled in with whatever type 261 00:13:03,278 --> 00:13:04,360 you give it here. 262 00:13:04,360 --> 00:13:08,230 And those holes are specified by this [T]. The way you can think 263 00:13:08,230 --> 00:13:09,550 of this T is 264 00:13:09,549 --> 00:13:10,609 it's the Type, 265 00:13:10,610 --> 00:13:11,810 that's why it's a T 266 00:13:11,809 --> 00:13:15,948 that this template is what we refer to instantiated as. You create one of these 267 00:13:15,948 --> 00:13:17,349 templates of this type. So I 268 00:13:17,350 --> 00:13:21,790 create an ArrayList of strings is the way programmers refer to it. Usually, you 269 00:13:21,789 --> 00:13:24,049 can think of this as meaning of 270 00:13:24,049 --> 00:13:25,589 and this as meaning nothing. 271 00:13:25,590 --> 00:13:29,190 So ArrayList of string is how we would actually say it. 272 00:13:29,190 --> 00:13:33,660 And when you create an ArrayList of string you get an Add method for this ArrayList 273 00:13:33,659 --> 00:13:34,919 of strings 274 00:13:34,919 --> 00:13:36,240 that's now 275 00:13:36,240 --> 00:13:39,320 [T] is replaced by 276 00:13:39,320 --> 00:13:43,550 string. So the method that it actually provides for you is an ad 277 00:13:43,549 --> 00:13:46,529 method that takes a string type. Okay? 278 00:13:46,529 --> 00:13:49,759 If I were to create an ArrayList of G ovals 279 00:13:49,759 --> 00:13:55,000 I would have an Add method for that class that's elements was a G oval. Okay? 280 00:13:55,000 --> 00:13:58,298 So it creates for you basically a whole class 281 00:13:58,298 --> 00:14:01,818 that has all the holes filled in with whatever type you specify here. Any 282 00:14:01,818 --> 00:14:03,409 questions about that? 283 00:14:03,409 --> 00:14:06,789 Kind of a critical concept, new in Java 5.0. It's new, it's dynamic, it's cool, 284 00:14:06,789 --> 00:14:07,929 it's funky. 285 00:14:07,929 --> 00:14:09,199 People like it, 286 00:14:09,200 --> 00:14:12,259 now you can, too. All right. So 287 00:14:12,259 --> 00:14:15,340 let me show you some of the methods that you get with this ArrayList kind of thing, 288 00:14:15,340 --> 00:14:18,470 and then we'll actually go into the details of using it. And you'll notice that when we 289 00:14:18,470 --> 00:14:19,639 talk about the methods, 290 00:14:19,639 --> 00:14:22,909 this [T] thing will show up all over the place. 291 00:14:22,909 --> 00:14:26,269 And the way you want to think about that is this is just getting replaced with 292 00:14:26,269 --> 00:14:29,049 whatever type you instantiate your ArrayList to be. 293 00:14:29,049 --> 00:14:31,329 So if we go to the computer real quick, 294 00:14:31,330 --> 00:14:34,570 drum roll please, it's the methods of 295 00:14:34,570 --> 00:14:38,610 the ArrayList. So what are the methods of the ArrayList? The 296 00:14:38,610 --> 00:14:40,769 first two methods are Add. 297 00:14:40,769 --> 00:14:44,809 What Add does is it takes in some element type of whatever type your 298 00:14:44,809 --> 00:14:48,338 ArrayList is instantiated to be, right, the [T] 299 00:14:48,339 --> 00:14:51,529 and it adds it to the end of the ArrayList. And you might say, "Hey, Miron, what does 300 00:14:51,528 --> 00:14:52,960 that return a bouillon," 301 00:14:52,960 --> 00:14:57,009 and it always returns true for the bouillon, so why does it return a bouillon at all? 302 00:14:57,009 --> 00:15:00,480 And about in a week it'll be clear why it always returns a bouillon. Okay? 303 00:15:00,480 --> 00:15:02,750 And why, in this case, it's always true, but 304 00:15:02,750 --> 00:15:05,860 as far as you're concerned you don't really care about the return type. 305 00:15:05,860 --> 00:15:08,908 You're not gonna do anything with it because it's always true. You're just gonna say Add 306 00:15:08,908 --> 00:15:11,408 and even though it's returning something to you, you're not gonna [inaudible]. It just adds 307 00:15:11,408 --> 00:15:15,038 an element to the end of the ArrayList, which means the ArrayList 308 00:15:15,038 --> 00:15:18,708 dynamically grows by one. Right? It's an array with dynamic size, so it just creates the space 309 00:15:18,708 --> 00:15:20,979 for you and adds the new elements to the end. 310 00:15:20,980 --> 00:15:24,480 You can also add in a particular position just like you could add an array at 311 00:15:24,480 --> 00:15:27,038 a particular position, you can do that in an array list 312 00:15:27,038 --> 00:15:31,288 by specifying the index and the element that you want to enter at that index, and it 313 00:15:31,288 --> 00:15:34,718 will just put that element in at that index for you and it's just kind of all 314 00:15:34,719 --> 00:15:37,740 abstracted in terms of how it does that. Okay? 315 00:15:37,740 --> 00:15:40,340 It's just growing its size as needed. 316 00:15:40,340 --> 00:15:43,509 Besides adding things you can remove from the ArrayList, right, which is something 317 00:15:43,509 --> 00:15:47,269 that's funky. You didn't really think about removing from an array. Right? An array you could sort of 318 00:15:47,269 --> 00:15:47,610 say, 319 00:15:47,610 --> 00:15:50,720 "Yeah, read that value and maybe I don't care about the value anymore," but 320 00:15:50,720 --> 00:15:52,370 you couldn't actually say, "Yeah, 321 00:15:52,370 --> 00:15:55,659 just get rid of that value completely, like, get rid of the space associated with 322 00:15:55,659 --> 00:15:57,149 that value." Okay? 323 00:15:57,149 --> 00:15:59,939 So when you say remove at a particular index, 324 00:15:59,940 --> 00:16:04,070 it actually removes that element and returns it to you from the ArrayList. 325 00:16:04,070 --> 00:16:07,560 And similarly, you can either remove or you 326 00:16:07,559 --> 00:16:11,349 give it an index and you say, "Give me back that particular element," or you can remove a particular 327 00:16:11,350 --> 00:16:15,279 element if it appears. So this returns a bouillon. So let's say you have an ArrayList 328 00:16:15,279 --> 00:16:16,458 of strings 329 00:16:16,458 --> 00:16:20,159 and one of your strings may be cs106a, and you're like, "Oh, 330 00:16:20,159 --> 00:16:22,649 cs106a, it was so near and dear to my heart 331 00:16:22,649 --> 00:16:26,740 until I had to take a midterm in the middle of an earthquake," 332 00:16:26,740 --> 00:16:28,428 remove cs106a. 333 00:16:28,428 --> 00:16:31,269 And then you try to do this for everyone on campus. Well, some people have 106a 334 00:16:31,269 --> 00:16:34,549 and if it appears it'll return true and remove it from the ArrayList, 335 00:16:34,549 --> 00:16:35,839 but some people don't. 336 00:16:35,840 --> 00:16:38,540 And it doesn't suddenly crash their program or whatever. When you come to 337 00:16:38,539 --> 00:16:42,480 that person and you call remove on cs106a, it just returns false to 338 00:16:42,480 --> 00:16:45,789 indicate, "Hey, cs106a wasn't in this ArrayList, I didn't make any 339 00:16:45,789 --> 00:16:47,389 changes to the ArrayList." Okay. 340 00:16:47,389 --> 00:16:50,899 And similarly, if you just get really angry or you decide, "Oh, I'm gonna take some time 341 00:16:50,899 --> 00:16:54,529 off from Standard, I want to just clear my ArrayList of classes," 342 00:16:54,529 --> 00:16:58,250 you can call "clear" which just removes everything in the ArrayList. It just 343 00:16:58,250 --> 00:17:02,440 kind of resets it to be a blank ArrayList. Okay? 344 00:17:02,440 --> 00:17:06,730 You can ask an ArrayList for its size, not particularly exciting, but very important to 345 00:17:06,730 --> 00:17:09,390 actually be able to do. It just returns how many elements are currently in the 346 00:17:09,390 --> 00:17:11,050 ArrayList. 347 00:17:11,049 --> 00:17:13,899 Besides putting elements in an ArrayList and removing them it wouldn't be much fun if 348 00:17:13,900 --> 00:17:17,769 you couldn't actually get an element, so getting an element at a particular index 349 00:17:17,769 --> 00:17:20,088 returns the object at that specified index. 350 00:17:20,088 --> 00:17:24,000 It doesn't remove it from the 351 00:17:24,000 --> 00:17:24,669 ArrayList, 352 00:17:24,669 --> 00:17:26,099 it just gives it back to you. 353 00:17:26,098 --> 00:17:30,470 So if there was an object or some element at position one, okay, 354 00:17:30,470 --> 00:17:33,720 and you call "Get" it remains at position one. If you call "remove" it's 355 00:17:33,720 --> 00:17:34,399 actually removed 356 00:17:34,398 --> 00:17:36,558 from the ArrayList. Okay. 357 00:17:36,558 --> 00:17:37,740 And then there's set, 358 00:17:37,740 --> 00:17:40,829 and so set you can actually say, "Hey, at some particular location 359 00:17:40,829 --> 00:17:44,019 I don't care what's there, just slam this thing I'm gonna give you." So what 360 00:17:44,019 --> 00:17:46,529 it does is you give it a particular index 361 00:17:46,529 --> 00:17:50,149 and you give it some particular value, and it puts that value at that index. So you 362 00:17:50,150 --> 00:17:54,570 can say, "Set at position one, you know, this string 106b because now after you've 363 00:17:54,569 --> 00:17:57,729 taken 106a, maybe next quarter you're taking 106b, 364 00:17:57,729 --> 00:17:58,980 and it goes, "That's great," 365 00:17:58,980 --> 00:18:02,710 and what it gives you back is the old value at that index. So it would give 366 00:18:02,710 --> 00:18:05,650 you back 106a if that was the old value that was there. Just in case 367 00:18:05,650 --> 00:18:06,750 you want to do sort of the 368 00:18:06,750 --> 00:18:10,500 quickie slight of hand change, presto change, stick something new in, get 369 00:18:10,500 --> 00:18:11,759 the old value out. 370 00:18:11,759 --> 00:18:14,650 You can do that if you want. That's just the way it is. 371 00:18:14,650 --> 00:18:15,830 Last but not least, a 372 00:18:15,829 --> 00:18:19,579 couple of things real quickly. You can get index of. This is kind of funky. This essentially does 373 00:18:19,579 --> 00:18:23,049 a search for you on the ArrayList. So you give it some particular value, 374 00:18:23,049 --> 00:18:24,680 like, you could say, "Hey, 375 00:18:24,680 --> 00:18:28,330 is 106a actually in my ArrayList," so you would pass it to string 376 00:18:28,329 --> 00:18:32,169 106a and it would return to you the index of the first occurrence of cs106a 377 00:18:32,170 --> 00:18:33,009 378 00:18:33,009 --> 00:18:34,460 if it was in your ArrayList 379 00:18:34,460 --> 00:18:38,130 and minus one otherwise. It's just kind of like the index of operation that you 380 00:18:38,130 --> 00:18:41,830 did on strings before when you called it with like [inaudible] or substrings. 381 00:18:41,829 --> 00:18:44,798 It's the same thing here except now you have a whole list of things, and that whole 382 00:18:44,798 --> 00:18:47,619 list of things can be any type that you want. 383 00:18:47,619 --> 00:18:50,259 Contains, the same kind of thing. It basically just returns a bouillon. You give it 384 00:18:50,259 --> 00:18:53,930 some value, and it says, "True or false, this value actually appeared in the list 385 00:18:53,930 --> 00:18:57,640 and is empty." It just returns true or false to let you know if the ArrayList contains any 386 00:18:57,640 --> 00:18:59,020 elements at all. Okay? 387 00:18:59,019 --> 00:19:04,299 So fairly generic, basic functionality and it's all kind of good and well and we're happy. So 388 00:19:04,299 --> 00:19:07,419 let's use some of that and actually write a program that makes use of an 389 00:19:07,420 --> 00:19:08,750 ArrayList 390 00:19:08,750 --> 00:19:12,240 together so we can see how some of this functionality actually looks, and then 391 00:19:12,240 --> 00:19:15,970 we'll try it out on the computer and try a few more advanced things with it. 392 00:19:15,970 --> 00:19:18,700 So here's our friend the Run Method. We got public, 393 00:19:18,700 --> 00:19:20,179 void, 394 00:19:20,179 --> 00:19:21,690 run; 395 00:19:21,690 --> 00:19:25,500 inside here we're gonna create a new ArrayList of strings. So I have ArrayList 396 00:19:25,500 --> 00:19:26,720 397 00:19:26,720 --> 00:19:27,970 of 398 00:19:27,970 --> 00:19:29,360 string, 399 00:19:29,359 --> 00:19:34,240 ignore that part, although you need it for the syntax, but we just don't say it as 400 00:19:34,240 --> 00:19:36,680 programmers. We say ArrayList of strings. 401 00:19:36,680 --> 00:19:40,799 My string list, I'm just calling it SList, is a new, very similar to what you saw 402 00:19:40,799 --> 00:19:41,629 there, 403 00:19:41,630 --> 00:19:43,250 ArrayList 404 00:19:43,250 --> 00:19:45,579 that holds strings 405 00:19:45,579 --> 00:19:49,009 and there's no parameters for the constructor, so I make sure to have 406 00:19:49,009 --> 00:19:50,289 the 407 00:19:50,289 --> 00:19:53,000 () even though it looks kind of funky. You have to have it. So I just create 408 00:19:53,000 --> 00:19:53,880 one of these, 409 00:19:53,880 --> 00:19:57,309 and now what I'm gonna do is just repeatedly read strings from the user 410 00:19:57,308 --> 00:20:01,139 until the user enters a blank line to indicate, "Hey, there's no more lines that I 411 00:20:01,140 --> 00:20:01,909 want to give you," 412 00:20:01,909 --> 00:20:04,140 and then I'm gonna write them all out. 413 00:20:04,140 --> 00:20:08,360 So I have some wild loop in here and I'm gonna do my friend the wild true loop, 414 00:20:08,359 --> 00:20:10,029 right, the old loop and a half construct where I'm 415 00:20:10,029 --> 00:20:12,099 gonna have some string line that 416 00:20:12,099 --> 00:20:15,980 I'm gonna read in from the user. So I'm just gonna use read 417 00:20:15,980 --> 00:20:16,430 line 418 00:20:16,430 --> 00:20:20,100 and to keep the prompt short, it'll just be a question mark. And I'll just assume the user 419 00:20:20,099 --> 00:20:21,559 knows to enter a line there. 420 00:20:21,559 --> 00:20:25,579 I want to check to see if that line actually contains anything so I check 421 00:20:25,579 --> 00:20:27,989 is the line dot equals 422 00:20:27,989 --> 00:20:31,220 to the empty string "" and if it is 423 00:20:31,220 --> 00:20:34,150 then I'm gonna break, which means the user just gave me a blank line so 424 00:20:34,150 --> 00:20:36,390 there are no more lines that they want to enter. 425 00:20:36,390 --> 00:20:40,540 And if they gave me a line that is not equal to the empty string, 426 00:20:40,539 --> 00:20:44,599 then I actually got some valid line. I want to stick that string inside my ArrayList 427 00:20:44,599 --> 00:20:47,939 at the end and tell my ArrayList to just kind of grow in size. 428 00:20:47,940 --> 00:20:50,779 So I do that by just saying SList 429 00:20:50,779 --> 00:20:52,069 dot Add, 430 00:20:52,069 --> 00:20:55,099 and you'll notice the parameter that Add's gonna take up there is something 431 00:20:55,099 --> 00:20:59,049 of whatever the type of the ArrayList is. So in this case, it's going to be a 432 00:20:59,049 --> 00:21:01,940 string so I can do an Add on line 433 00:21:01,940 --> 00:21:04,320 whose type is string and I'm perfectly fine. 434 00:21:04,319 --> 00:21:07,548 And this will return to me true, but I don't care because I know it always returns 435 00:21:07,548 --> 00:21:08,279 true. 436 00:21:08,279 --> 00:21:11,730 So I'm just not even gonna assign whatever turns to many variables. 437 00:21:11,730 --> 00:21:12,409 And this 438 00:21:12,409 --> 00:21:15,909 will keep reading strings from the user until the user gives me a blank line to 439 00:21:15,909 --> 00:21:17,470 indicate that they're done, 440 00:21:17,470 --> 00:21:20,960 and then I'll write a little loop so this is just a continuation of the program to 441 00:21:20,960 --> 00:21:24,308 actually print all those strings on the screen so you can see what that looks 442 00:21:24,308 --> 00:21:25,819 like. Okay? 443 00:21:25,819 --> 00:21:29,879 So I have a little four loop over here for I = - forgot the 444 00:21:29,880 --> 00:21:32,840 [inaudible] 445 00:21:32,839 --> 00:21:33,879 = 0. 446 00:21:33,880 --> 00:21:35,910 "I" is less than - I need to know 447 00:21:35,910 --> 00:21:39,310 how many strings are actually entered into my ArrayList. Okay. 448 00:21:39,309 --> 00:21:42,288 Well, I can use my methods over there to figure that out with size. 449 00:21:42,288 --> 00:21:45,599 So I just say SList dot 450 00:21:45,599 --> 00:21:47,039 size 451 00:21:47,039 --> 00:21:50,180 I ++, 452 00:21:50,180 --> 00:21:53,180 and inside here I'm just going to print line 453 00:21:53,180 --> 00:21:56,610 and the line that I'm going to print is sequentially I want to go through my 454 00:21:56,609 --> 00:22:00,349 ArrayList and get strings one at a time at the given index. 455 00:22:00,349 --> 00:22:01,538 So how do I do that? 456 00:22:01,538 --> 00:22:08,538 I just say string list, and what's the method I use, - [inaudible] 457 00:22:08,920 --> 00:22:11,330 the single syllable gets candy. 458 00:22:11,329 --> 00:22:13,449 And so did everyone else. 459 00:22:13,450 --> 00:22:15,179 Mostly the empty seats - 460 00:22:15,179 --> 00:22:16,640 get 461 00:22:16,640 --> 00:22:20,100 "I." So that will get the "I" string. I 462 00:22:20,099 --> 00:22:21,759 got my parens lined up there 463 00:22:21,759 --> 00:22:25,099 and that will print everything out. So it just gets a bunch of lines and the user prints 464 00:22:25,099 --> 00:22:27,089 out those lines on the screen. Okay? 465 00:22:27,089 --> 00:22:30,709 But notice I don't need to declare an initial size for the ArrayList. I don't 466 00:22:30,710 --> 00:22:31,850 need to 467 00:22:31,849 --> 00:22:35,418 worry about is it big enough, did I over write the end of it, do I get the 468 00:22:35,419 --> 00:22:36,960 big 469 00:22:36,960 --> 00:22:39,909 mean evil exception going off the end of the ArrayList? 470 00:22:39,909 --> 00:22:43,740 No, it just keeps expanding space as long as I need space. That's why it's so versatile to use and 471 00:22:43,740 --> 00:22:44,618 when I'm done, 472 00:22:44,618 --> 00:22:47,419 I can just go ahead and say, "Hey, what's your size now?" 473 00:22:47,420 --> 00:22:47,890 And then 474 00:22:47,890 --> 00:22:51,980 I can add more if I want, so let me show you a generalization of this program that 475 00:22:51,980 --> 00:22:54,150 we just wrote. 476 00:22:54,150 --> 00:22:57,960 So if we come back to our friend - 477 00:22:57,960 --> 00:22:59,819 all done with you power point. 478 00:22:59,819 --> 00:23:02,980 Thanks for playing, and don't save. All right. 479 00:23:02,980 --> 00:23:05,429 So we come over to our friend eclipse, 480 00:23:05,429 --> 00:23:08,860 and here's a simple ArrayList example oddly enough 481 00:23:08,859 --> 00:23:11,419 named simple ArrayList dot Java. 482 00:23:11,420 --> 00:23:13,210 So what this is gonna do 483 00:23:13,210 --> 00:23:16,710 is gonna pass around the ArrayList as a parameter 484 00:23:16,710 --> 00:23:19,329 doing the same functionality, but I just want to show you what it looks like when 485 00:23:19,329 --> 00:23:23,618 we de-compose the program a little bit and how we pass ArrayLists as parameters. 486 00:23:23,618 --> 00:23:27,158 So I create an ArrayList of strings here, the same syntax that I had before. 487 00:23:27,159 --> 00:23:29,880 I just called it List instead of SList, 488 00:23:29,880 --> 00:23:34,330 and I want to read in that list from the user so I'm going to pass as a parameter 489 00:23:34,329 --> 00:23:35,759 the whole ArrayList. 490 00:23:35,759 --> 00:23:39,798 The way I pass the whole Array is just by specifying the name of the 491 00:23:39,798 --> 00:23:41,869 parameter, which in this case, is list. 492 00:23:41,869 --> 00:23:43,500 So I'm gonna read a list from the user. 493 00:23:43,500 --> 00:23:47,640 So what does read list do? Here's the whole read list function. Now here's the funky thing. If you can get 494 00:23:47,640 --> 00:23:49,900 to the computer that would be extremely useful. 495 00:23:49,900 --> 00:23:53,470 The read list function - how do I pass an ArrayList as a parameter? 496 00:23:53,470 --> 00:23:57,620 I just say ArrayList and List and you might look at this and say, "Oh, 497 00:23:57,619 --> 00:23:58,819 but Miron, 498 00:23:58,819 --> 00:24:03,029 you're not specifying what kind of ArrayList it is. Is it an ArrayList of strings, 499 00:24:03,029 --> 00:24:04,269 an ArrayList of 500 00:24:04,269 --> 00:24:05,750 integers," the 501 00:24:05,750 --> 00:24:09,680 function or the method doesn't care. All it cares about is you're giving me an 502 00:24:09,680 --> 00:24:10,740 ArrayList, 503 00:24:10,740 --> 00:24:13,380 and I'm gonna assume you're gonna do sort of the right thing with it in terms of putting 504 00:24:13,380 --> 00:24:16,260 stuff in and taking stuff out of the ArrayList. But 505 00:24:16,259 --> 00:24:20,420 you're just giving me an ArrayList, so you do not specify as a parameter 506 00:24:20,420 --> 00:24:25,450 the type that that ArrayList stores. It knows because the actual object you pass 507 00:24:25,450 --> 00:24:28,569 underneath the hood knows, "Hey, I'm an ArrayList of strings." So if you try to stick 508 00:24:28,569 --> 00:24:30,819 something in it that isn't a string, it'll get upset. 509 00:24:30,819 --> 00:24:34,608 But it just trusts you when you actually pass it as a parameter. So here we say, 510 00:24:34,608 --> 00:24:35,949 "While true, 511 00:24:35,950 --> 00:24:39,319 string item =s read line," so here I'm just calling it item instead of 512 00:24:39,319 --> 00:24:39,569 line. 513 00:24:39,529 --> 00:24:41,750 I read a line from the user. If that 514 00:24:41,750 --> 00:24:43,250 line happens to be 515 00:24:43,250 --> 00:24:44,240 equal to 516 00:24:44,240 --> 00:24:45,028 "" 517 00:24:45,028 --> 00:24:47,160 I'm done and I break out of the loop. 518 00:24:47,160 --> 00:24:48,920 Otherwise, 519 00:24:48,920 --> 00:24:52,308 what I'm going to do is add the item that was just read, which is the string, 520 00:24:52,308 --> 00:24:55,259 to the list. So it's exactly the same code that I had here, 521 00:24:55,259 --> 00:24:57,759 I just have decomposed it out into a function. 522 00:24:57,759 --> 00:25:00,279 Now notice I don't return anything from this method, 523 00:25:00,279 --> 00:25:03,869 and the reason why I don't return anything from the method is an ArrayList, at the 524 00:25:03,869 --> 00:25:05,750 end of the day, is an object. 525 00:25:05,750 --> 00:25:10,609 When I pass an object what happens to changes I make to the object? 526 00:25:10,609 --> 00:25:14,000 They persist, right? It's an object. It's the like the Mona Lisa that's being shipped 527 00:25:14,000 --> 00:25:15,490 around in the truck. 528 00:25:15,490 --> 00:25:19,440 If I go and slice the truck in half, I slice the Mona Lisa in half because I really 529 00:25:19,440 --> 00:25:21,100 have the real object. 530 00:25:21,099 --> 00:25:24,559 And so I don't need to return anything because any changes I make to this ArrayList, 531 00:25:24,559 --> 00:25:27,259 because the ArrayList itself is an object, 532 00:25:27,259 --> 00:25:29,379 those changes actually persist. 533 00:25:29,380 --> 00:25:31,880 And so whether or not you think of an ArrayList as 534 00:25:31,880 --> 00:25:36,260 an object or as an array, in both cases if I pass an array to a 535 00:25:36,259 --> 00:25:39,648 method or I pass an object to a method, in both cases the changes persist, so it 536 00:25:39,648 --> 00:25:42,518 doesn't matter how you think of an ArrayList. If it's easier for you to think of it 537 00:25:42,519 --> 00:25:45,929 as just an array, think of it as an array and the changes persist, and if it's easier 538 00:25:45,929 --> 00:25:49,830 for you to think of an object, which is what it really is, 539 00:25:49,829 --> 00:25:53,480 then changes persist as well. And the truth underneath the hood is to 540 00:25:53,480 --> 00:25:54,649 be real quiet. 541 00:25:54,648 --> 00:25:59,569 Array is just a regular ray are objects too. Just so you know. 542 00:25:59,569 --> 00:26:01,658 But you don't need to worry about that. All right. Just 543 00:26:01,659 --> 00:26:03,810 think of them as this thing called an array. 544 00:26:03,809 --> 00:26:06,679 So that reads a list. And then after we read the list from the user, and all 545 00:26:06,680 --> 00:26:11,250 these changes that we made to list persist, I print out the ArrayList, 546 00:26:11,250 --> 00:26:15,359 and I'll print out ArrayList. Okay. And I'll print out an ArrayList again, it gets the ArrayList as a parameter 547 00:26:15,359 --> 00:26:19,979 and it just says the list contains list dot size elements, so it tells me how many 548 00:26:19,980 --> 00:26:21,269 elements are currently in the list 549 00:26:21,269 --> 00:26:24,650 and then it does the little four loop that I just drew up on the board going through all 550 00:26:24,650 --> 00:26:29,060 the elements from zero up to size, actually size minus one, and writing them 551 00:26:29,059 --> 00:26:30,509 all out on the screen. 552 00:26:30,509 --> 00:26:33,408 And I'm gonna do this twice so I'm gonna read the list, 553 00:26:33,409 --> 00:26:36,409 print it out and then I'm gonna read to the same list just to show you that 554 00:26:36,409 --> 00:26:40,110 the list is gonna continue to grow when I do that, so let me run this. I 555 00:26:40,109 --> 00:26:42,199 already compiled it. 556 00:26:42,200 --> 00:26:45,090 So it's asking for lines, cs106a. That's 557 00:26:45,089 --> 00:26:47,829 over in the psych department. 558 00:26:47,829 --> 00:26:49,859 CS106a 559 00:26:49,859 --> 00:26:50,899 rocks. 560 00:26:50,900 --> 00:26:54,269 And that's the end. It says, "List contains two elements, cs106a and rocks," 561 00:26:54,269 --> 00:27:00,500 and I'm, like, "Oh, yeah, I forgot to add heavily. 562 00:27:00,500 --> 00:27:03,548 And now the list contains four elements because I continued to add stuff to 563 00:27:03,548 --> 00:27:07,190 that same list. It just continues to append onto the end. Okay? Are there 564 00:27:07,190 --> 00:27:11,039 any questions about that? It's not like you just add and after you add and you stop adding 565 00:27:11,039 --> 00:27:14,649 or it's just frozen, it's not frozen. You can just keep adding more stuff if you want. 566 00:27:14,650 --> 00:27:16,390 Okay? Any questions about that? 567 00:27:16,390 --> 00:27:19,500 Are we feeling okay about the ArrayList? 568 00:27:19,500 --> 00:27:22,940 Now here's the funky thing. At this point, you should be going, "Hey, 569 00:27:22,940 --> 00:27:24,429 ArrayList, kind of cool. 570 00:27:24,429 --> 00:27:27,399 How come this time, Miron, you've been doing ArrayLists with strings? 571 00:27:27,398 --> 00:27:31,349 I thought the simple type was like ints. Why weren't we doing ArrayLists of 572 00:27:31,349 --> 00:27:35,119 ints? Any guesses? No guesses? No guesses. 573 00:27:35,119 --> 00:27:38,459 All right. I'll give you a hint. 574 00:27:38,460 --> 00:27:43,250 It's a popular science fiction TV show from the 575 00:27:43,250 --> 00:27:47,890 70s. Actually, 60s, I would imagine, 60s and 70s. I think it's 576 00:27:47,890 --> 00:27:51,830 actually 577 00:27:51,829 --> 00:27:52,428 578 00:27:52,429 --> 00:27:53,230 60s. 579 00:27:53,230 --> 00:27:55,339 Keep thinking. It'll get you closer. 580 00:27:55,339 --> 00:27:57,939 Here's the funky things about ArrayLists. 581 00:27:57,940 --> 00:28:00,370 An ArrayList, as a template, 582 00:28:00,369 --> 00:28:04,748 can only be instantiated, which means its type can only be specified 583 00:28:04,749 --> 00:28:10,430 to be objects. So ArrayLists hold objects. 584 00:28:10,430 --> 00:28:13,200 As we talked about in the past 585 00:28:13,200 --> 00:28:14,069 [inaudible] 586 00:28:14,069 --> 00:28:15,649 double 587 00:28:15,650 --> 00:28:20,560 bouillon and are friend the little care always comes at the end, just kind of 588 00:28:20,559 --> 00:28:23,980 struggling along, are not objects. They're primitive types. 589 00:28:23,980 --> 00:28:27,970 So I cannot in fact have an ArrayList of ints. If I try to say ArrayList [ints] 590 00:28:27,970 --> 00:28:29,190 591 00:28:29,190 --> 00:28:32,070 I get an error because an ints is not an object. 592 00:28:32,069 --> 00:28:36,529 So in order to get around that - remember a few lecturers ago we talked about, "Hey, 593 00:28:36,529 --> 00:28:39,359 for everyone of the primitive types 594 00:28:39,359 --> 00:28:43,369 there's an equivalence class type, so these are the primitives and the equivalent class types, 595 00:28:43,369 --> 00:28:48,889 which are called wrapper classes because they kind of wrap around these. The 596 00:28:48,890 --> 00:28:55,240 wrapper classes are INTEGER, W, BOUILLON 597 00:28:55,240 --> 00:29:00,450 and then character all written out are the wrapper 598 00:29:00,450 --> 00:29:02,048 classes. 599 00:29:02,048 --> 00:29:05,589 So if I actually want to have an ArrayList that holds integers, 600 00:29:05,589 --> 00:29:10,949 it can't hold ints. I need to create an ArrayList that holds integers. It actually 601 00:29:10,950 --> 00:29:16,149 has to hold objects so it has to hold instances of some class. Okay. Now, 602 00:29:16,148 --> 00:29:20,099 in the bad old days it was Java, you know, 603 00:29:20,099 --> 00:29:22,019 pre-Java 5.0 604 00:29:22,019 --> 00:29:24,788 this made ArrayLists really cumbersome to use 605 00:29:24,788 --> 00:29:28,349 because every time you had some integer somewhere you said, "Hey, you know what, 606 00:29:28,349 --> 00:29:32,859 I have this thing called int X and maybe it's value I said it originally to 607 00:29:32,859 --> 00:29:36,979 be five, and now I want to stick this inside some ArrayList of integers that I 608 00:29:36,980 --> 00:29:38,339 created somewhere 609 00:29:38,339 --> 00:29:42,449 and I can't do that. So I need to actually go and create a new integer 610 00:29:42,450 --> 00:29:42,848 object 611 00:29:42,848 --> 00:29:46,009 and assign the value for that integer object to be what X is and it just 612 00:29:46,009 --> 00:29:50,670 became a huge pain. So you actually need to do something like this. You need to say, "Integer Y 613 00:29:50,670 --> 00:29:51,769 = 614 00:29:51,769 --> 00:29:53,460 new 615 00:29:53,460 --> 00:29:54,250 integer," 616 00:29:54,250 --> 00:29:56,579 and then give it the value X 617 00:29:56,579 --> 00:29:59,730 because what you need to do is create a new instance of the integer 618 00:29:59,730 --> 00:30:02,569 class and its initial value you gave it as an int, 619 00:30:02,569 --> 00:30:06,298 and then it created this nice little box for you which was the 620 00:30:06,298 --> 00:30:08,710 integer object 621 00:30:08,710 --> 00:30:12,700 and inside the box there is this little space for the actual value and it held the five 622 00:30:12,700 --> 00:30:13,960 in there. 623 00:30:13,960 --> 00:30:17,140 And that was a pain to have to do this every time you wanted to put some object 624 00:30:17,140 --> 00:30:18,320 into an 625 00:30:18,319 --> 00:30:19,558 ArrayList of integers. 626 00:30:19,558 --> 00:30:22,490 And similarly, if you wanted to get something out 627 00:30:22,490 --> 00:30:25,450 you had to do something equally cumbersome, which if I wanted to get the 628 00:30:25,450 --> 00:30:28,269 value out. So if I wanted to have some integer Z here and I want 629 00:30:28,269 --> 00:30:29,289 to say, "Hey, 630 00:30:29,289 --> 00:30:30,799 get the value of that integer, 631 00:30:30,799 --> 00:30:35,019 I can't just say Z = Y, at least I couldn't in the bad old days." I needed to 632 00:30:35,019 --> 00:30:36,429 actually say Y 633 00:30:36,429 --> 00:30:39,900 dot ints value and call a method on it. And 634 00:30:39,900 --> 00:30:42,880 then what this gave me back from that object Y 635 00:30:42,880 --> 00:30:48,200 was its actual value of an integer, so it would give me back the five as an integer. Okay. 636 00:30:48,200 --> 00:30:50,840 All that changed in Java 5.0. 637 00:30:50,839 --> 00:30:55,019 So the process of essentially creating an object 638 00:30:55,019 --> 00:30:56,839 around a particular primitive, 639 00:30:56,839 --> 00:31:00,000 right, so we had our little primitive int five over here, and what this was 640 00:31:00,000 --> 00:31:00,849 basically doing 641 00:31:00,849 --> 00:31:05,469 was creating Y to be an object around the five. This process 642 00:31:05,470 --> 00:31:07,250 is still called 643 00:31:07,250 --> 00:31:07,808 "boxing" 644 00:31:07,808 --> 00:31:11,720 because basically what you were doing was drawing another box around your value. 645 00:31:11,720 --> 00:31:14,140 And this process over here 646 00:31:14,140 --> 00:31:16,300 of going from 647 00:31:16,299 --> 00:31:20,129 the box that you had to getting the value out of the box, interestingly 648 00:31:20,130 --> 00:31:22,830 enough, was called unboxing 649 00:31:22,829 --> 00:31:25,379 because basically when you wanted to get the value out, 650 00:31:25,380 --> 00:31:28,860 you cared about the value inside and essentially you wanted to erase the box 651 00:31:28,859 --> 00:31:30,789 that was around it. Okay. 652 00:31:30,789 --> 00:31:31,979 This whole 653 00:31:31,979 --> 00:31:36,009 boxing and unboxing happens for you automatically in Java 5.0 and 654 00:31:36,009 --> 00:31:36,630 later, 655 00:31:36,630 --> 00:31:37,650 which means 656 00:31:37,650 --> 00:31:40,610 if I create an ArrayList of integers 657 00:31:40,609 --> 00:31:43,750 658 00:31:43,750 --> 00:31:47,630 I can just add an int to it directly. So 659 00:31:47,630 --> 00:31:50,040 if I were to have an 660 00:31:50,039 --> 00:31:52,329 ArrayList 661 00:31:52,329 --> 00:31:56,539 it still needs to hold object of the integer class, so I still can't say an 662 00:31:56,539 --> 00:31:58,109 ArrayList of ints, 663 00:31:58,109 --> 00:32:00,819 and I'll call this IList 664 00:32:00,819 --> 00:32:01,629 = 665 00:32:01,630 --> 00:32:03,330 new 666 00:32:03,329 --> 00:32:08,058 ArrayList integer, 667 00:32:08,058 --> 00:32:13,920 so I create one of these things. Now I can say IList 668 00:32:13,920 --> 00:32:16,890 dot add and give it 669 00:32:16,890 --> 00:32:21,680 X directly where here I might have int X = five, and you would say, "But 670 00:32:21,680 --> 00:32:25,460 Miron, you told me this ArrayList can only store integer objects and you're 671 00:32:25,460 --> 00:32:29,440 passing an ints. You told me that doesn't work." Well, 672 00:32:29,440 --> 00:32:32,058 this is what Java 5.0 is doing for you automatically. It says, "Hey, 673 00:32:32,058 --> 00:32:35,158 this things expecting an integer and you're giving me an int," 674 00:32:35,159 --> 00:32:38,179 which means you really need to box it up. It's kind of like 675 00:32:38,179 --> 00:32:41,419 you finished your eating and you still have some food left and you want to 676 00:32:41,419 --> 00:32:43,639 store that food somewhere, what do you need to do? It's 677 00:32:43,638 --> 00:32:45,199 got to go in a box. 678 00:32:45,200 --> 00:32:48,850 And before you used to have to ask like the waiter or the waitress to bring you a box 679 00:32:48,849 --> 00:32:51,990 and they would create it in the backroom somewhere, probably out of other 680 00:32:51,990 --> 00:32:53,190 food that wasn't eaten, 681 00:32:53,190 --> 00:32:55,890 and then you would get the box and you'd put your value in, you could finally 682 00:32:55,890 --> 00:32:57,679 go and store it at home. 683 00:32:57,679 --> 00:33:01,740 And now Java just says, "Hey, you know what, people were asking for the box so often, 684 00:33:01,740 --> 00:33:05,490 yeah, if you need that food stored in a box, I'll just put the box on it for you, and 685 00:33:05,490 --> 00:33:10,019 then when you want to get it out over here," like you want to 686 00:33:10,019 --> 00:33:14,220 say, "int is Z = IList dot [inaudible], the zeros elements." 687 00:33:14,220 --> 00:33:14,960 Before 688 00:33:14,960 --> 00:33:18,710 you would just get the box, right, because that's all this thing is storing are 689 00:33:18,710 --> 00:33:20,200 boxes that hold integers. 690 00:33:20,200 --> 00:33:24,970 Now I'll actually take the box out for you and let you eat the food automatically. Okay. 691 00:33:24,970 --> 00:33:27,440 So it does that for you automatically, which 692 00:33:27,440 --> 00:33:29,660 makes these things a lot more useful. 693 00:33:29,660 --> 00:33:32,910 And the thing to keep in mind is that all these wrappers classes are still 694 00:33:32,910 --> 00:33:37,160 immutable objects. They're immutable in the same sense that strings are immutable. 695 00:33:37,160 --> 00:33:39,429 When you create one of these guys, 696 00:33:39,429 --> 00:33:42,140 like this guy Y has the value five, 697 00:33:42,140 --> 00:33:46,480 its value is five. You can't go back and say, "Hey, set its value now to be six," because 698 00:33:46,480 --> 00:33:49,240 it does not have a method to set its value. 699 00:33:49,240 --> 00:33:52,249 It can get you its value by asking for its integer value, 700 00:33:52,249 --> 00:33:55,139 but there is no way to set its value. If you now want to create some new 701 00:33:55,138 --> 00:33:56,558 integer that's value is six, then 702 00:33:56,558 --> 00:33:58,369 you need to create a new integer 703 00:33:58,369 --> 00:34:02,089 and actually say what's its value, its Y + 1. Okay. 704 00:34:02,089 --> 00:34:05,029 Just in the same with strings you couldn't change a string in place. If you 705 00:34:05,029 --> 00:34:06,069 wanted to actually change 706 00:34:06,069 --> 00:34:09,578 the string, what you really needed to do was create a new string that copied over 707 00:34:09,579 --> 00:34:12,460 all characters from the old string that you cared about and potentially 708 00:34:12,460 --> 00:34:14,909 made some other changes. Okay. So 709 00:34:14,909 --> 00:34:18,568 the wrapper classes are immutable in the same way strings are immutable. Once 710 00:34:18,568 --> 00:34:22,009 you set their values, it does not change. If you want to make it look like its 711 00:34:22,009 --> 00:34:25,639 really changing what you do is you create a new one and you would assign it to 712 00:34:25,639 --> 00:34:29,219 overwrite the value of the old one. Okay. 713 00:34:29,219 --> 00:34:29,809 So 714 00:34:29,809 --> 00:34:33,079 let me show you an example with that. 715 00:34:33,079 --> 00:34:37,028 And we'll use our same little code here and this is gonna be so much fun all 716 00:34:37,028 --> 00:34:40,458 we're gonna do is say, "Hey, you know what, rather than having an ArrayList of 717 00:34:40,458 --> 00:34:42,949 strings, I'm gonna have an ArrayList of integers." 718 00:34:42,949 --> 00:34:46,989 And you're gonna say, "But Miron, this is gonna make your whole program blow up." Well, 719 00:34:46,989 --> 00:34:50,338 read list doesn't care what type of ArrayList is getting passed in here. It's 720 00:34:50,338 --> 00:34:53,918 just getting passed an ArrayList. I don't want to put strings in there so 721 00:34:53,918 --> 00:34:56,129 I'm gonna change my item to be an int 722 00:34:56,130 --> 00:34:58,528 and I'll read in ints from the user, 723 00:34:58,528 --> 00:35:01,340 and basically the way I'm gonna stop is if that int 724 00:35:01,340 --> 00:35:03,369 is == to zero, 725 00:35:03,369 --> 00:35:06,129 and Add actually will just remain the same. 726 00:35:06,128 --> 00:35:08,699 And here's the coolest part of all. 727 00:35:08,699 --> 00:35:11,019 Prints ArrayList doesn't change at all 728 00:35:11,018 --> 00:35:14,338 even though I just went from strings to integers. Why does it not change at all? Because this ArrayList parameter here doesn't care what type you're passing 729 00:35:14,338 --> 00:35:17,958 in. 730 00:35:17,958 --> 00:35:21,308 The list contains the size method doesn't care what type is actually in 731 00:35:21,309 --> 00:35:26,019 the list, this list outside doesn't care what type is in the list. And when I get the 732 00:35:26,018 --> 00:35:28,968 element from the list, what I'm getting is an integer object, 733 00:35:28,969 --> 00:35:32,559 and when I go to print it out, it automatically gets unboxed for me to get 734 00:35:32,559 --> 00:35:35,820 turned into an integer and printed out. So even though I changed 735 00:35:35,820 --> 00:35:39,140 from an ArrayList of strings to an ArrayList of integers because I never 736 00:35:39,139 --> 00:35:43,059 specify the type here and that's just the way life is. That function will just work on 737 00:35:43,059 --> 00:35:44,259 change. Okay. 738 00:35:44,259 --> 00:35:48,259 So let me run this program again. 739 00:35:48,259 --> 00:35:50,358 740 00:35:50,358 --> 00:35:53,549 Yes, go ahead and save and do all the happy things you need to do. 741 00:35:53,550 --> 00:35:55,278 One, two, three 742 00:35:55,278 --> 00:35:59,269 - [inaudible] numeric format, right, it wasn't in line and it wanted a zero. 743 00:35:59,269 --> 00:36:03,150 Now the list contains three elements and then I'll add four and five and then give it a zero and 744 00:36:03,150 --> 00:36:05,660 now the list contains five elements. Okay. 745 00:36:05,659 --> 00:36:08,989 Again, the beauty of object drawing and programming and the beauty of templates. 746 00:36:08,989 --> 00:36:11,409 Some things can just be reused even though you change the type, and you just 747 00:36:11,409 --> 00:36:15,038 need to be careful what you're actually storing and not storing. Okay. 748 00:36:15,039 --> 00:36:19,079 So any questions about that? All right. 749 00:36:19,079 --> 00:36:20,999 Another quick example. 750 00:36:20,998 --> 00:36:24,198 When we talked about arrays I told you, "Hey, you know what, you can have an array 751 00:36:24,199 --> 00:36:25,980 that contains 752 00:36:25,980 --> 00:36:30,130 objects in it too." You can have an array of G objects or an array of G ovals or 753 00:36:30,130 --> 00:36:31,139 whatever you want, and 754 00:36:31,139 --> 00:36:32,349 that's perfectly fine. 755 00:36:32,349 --> 00:36:35,109 And you can do the same thing with ArrayLists. As a matter of a fact, with an 756 00:36:35,108 --> 00:36:38,420 ArrayList you don't need to worry about all this boxing/unboxing stuff 757 00:36:38,420 --> 00:36:42,240 because if you have an ArrayList of let's say G ovals or G [inaudible] or 758 00:36:42,239 --> 00:36:43,338 whatever the case may be. 759 00:36:43,338 --> 00:36:48,239 Those are already objects so there's no boxing or unboxing that needs to go on. Okay. 760 00:36:48,239 --> 00:36:53,679 So let's say I was gonna have an array of G labels, I could say G label - 761 00:36:53,679 --> 00:36:56,829 actually, let me show you this first of all in just the case of regular arrays and 762 00:36:56,829 --> 00:37:01,109 then we'll move on. So I can have an array of labels. 763 00:37:01,108 --> 00:37:04,369 So here is labels and I say something like new 764 00:37:04,369 --> 00:37:05,539 G 765 00:37:05,539 --> 00:37:06,469 label, 766 00:37:06,469 --> 00:37:10,610 it has four items in it and what I get here is 767 00:37:10,610 --> 00:37:15,190 four elements of my array that all start off no because none of them have been initialized. 768 00:37:15,190 --> 00:37:16,309 Okay. 769 00:37:16,309 --> 00:37:18,069 That's what I got in the array world. 770 00:37:18,068 --> 00:37:19,159 If in the 771 00:37:19,159 --> 00:37:24,428 ArrayList world I create an ArrayList of G labels I would say something like 772 00:37:24,429 --> 00:37:29,068 ArrayList that's going to store G label, 773 00:37:29,068 --> 00:37:31,400 and I'll call this my G list 774 00:37:31,400 --> 00:37:34,400 or my GL 775 00:37:34,400 --> 00:37:37,099 = new 776 00:37:37,099 --> 00:37:39,099 ArrayList 777 00:37:39,099 --> 00:37:40,039 of 778 00:37:40,039 --> 00:37:42,689 (G label). 779 00:37:42,688 --> 00:37:44,190 780 00:37:44,190 --> 00:37:47,059 This creates for me an empty list of 781 00:37:47,059 --> 00:37:48,479 G label objects. 782 00:37:48,478 --> 00:37:50,480 If I now want to add G labels 783 00:37:50,480 --> 00:37:55,119 I still need to create the actual underlying G label object and 784 00:37:55,119 --> 00:37:59,210 then add it to this ArrayList, much in the same way that with a regular list that when I 785 00:37:59,210 --> 00:38:00,099 created the 786 00:38:00,099 --> 00:38:00,940 array, 787 00:38:00,940 --> 00:38:04,599 all I got was just an array of no. So if I wanted to have the individual 788 00:38:04,599 --> 00:38:06,690 elements, I still needed to call a new 789 00:38:06,690 --> 00:38:10,530 on a G label for each one of the elements that I wanted to create. Same kind of 790 00:38:10,530 --> 00:38:13,480 idea going on with ArrayLists. All I get is an empty list 791 00:38:13,480 --> 00:38:17,579 and every time I want to add a new G label I should've created that G label 792 00:38:17,579 --> 00:38:18,958 object already. Okay. 793 00:38:18,958 --> 00:38:22,278 So let me show you an example of this 794 00:38:22,278 --> 00:38:26,018 with another funky program we call graphic numbers, or I call it graphic numbers. 795 00:38:26,018 --> 00:38:27,508 I just wrote it this morning, 796 00:38:27,509 --> 00:38:30,898 and all this thing is gonna do is listen for the mouse. 797 00:38:30,898 --> 00:38:34,498 Okay. All of the activities are going on when I click the mouse. So what's it gonna do? 798 00:38:34,498 --> 00:38:36,468 What it's gonna do when the moue is clicked - 799 00:38:36,469 --> 00:38:40,028 well, before the mouse is clicked I have some instance variable in here. The 800 00:38:40,028 --> 00:38:43,139 instance variable I have in here is this thing that I just showed you. I have an 801 00:38:43,139 --> 00:38:46,229 ArrayList, it's gonna be private, so it's gonna be within my object, 802 00:38:46,228 --> 00:38:48,730 it's an ArrayList called labels 803 00:38:48,730 --> 00:38:53,329 that holds G labels. So what I get when this line executes, when the 804 00:38:53,329 --> 00:38:54,710 program starts running, 805 00:38:54,710 --> 00:38:59,099 is I get an empty ArrayList of G labels. There's no G labels in it. 806 00:38:59,099 --> 00:39:02,528 The way I'm gonna add G labels is every time - this is the whole program - every 807 00:39:02,528 --> 00:39:06,358 time the user clicks the mouse I'm gonna create a new G label and add it to the 808 00:39:06,358 --> 00:39:07,588 list. Okay. 809 00:39:07,588 --> 00:39:10,889 So what I do when the user clicks the mouse is I'm gonna create a new 810 00:39:10,889 --> 00:39:15,768 G label called "lab" and the label or the text that's actually associated with this label 811 00:39:15,768 --> 00:39:20,188 is just the pounds sign or the number sign and then however many labels are 812 00:39:20,188 --> 00:39:23,259 currently in my list, which means at first it's gonna be zero so it's gonna give 813 00:39:23,259 --> 00:39:26,869 me a zero and the labels gonna be number zero, and the next time it's gonna 814 00:39:26,869 --> 00:39:29,999 be number one, and the next time it's gonna number two, etcetera. But the 815 00:39:29,998 --> 00:39:31,548 first time it'll be number zero. 816 00:39:31,548 --> 00:39:35,728 I set the fonts on that label to be Courier 18 because as the years are 817 00:39:35,728 --> 00:39:39,118 passing my eyes are having problems so I need the font to be big. 818 00:39:39,119 --> 00:39:41,479 And then what happens - here's the funky thing that I want to do. 819 00:39:41,478 --> 00:39:43,278 I want to say, "You know what, 820 00:39:43,278 --> 00:39:46,268 where am I gonna put this on the screen," I always want to put the brand 821 00:39:46,268 --> 00:39:50,348 new label, like, when it's born in the same place on the screen, but the problem 822 00:39:50,349 --> 00:39:53,669 is if I put number zero there and then when I'm ready to put number one, if I 823 00:39:53,668 --> 00:39:56,128 put it right on top of number zero it's gonna get real ugly and it's 824 00:39:56,128 --> 00:39:57,460 gonna be a mess, it's 825 00:39:57,460 --> 00:40:00,260 like dogs and cats sleeping together, and the whole thing is just not gonna work 826 00:40:00,260 --> 00:40:01,140 out. 827 00:40:01,139 --> 00:40:02,659 So what am I gonna do? 828 00:40:02,659 --> 00:40:06,018 I'm gonna say, "Hey, why don't I take all the other numbers I had before and move 829 00:40:06,018 --> 00:40:09,239 them down one line so the new guy will always show up on top, 830 00:40:09,239 --> 00:40:12,309 and then when there's gonna be a new one, everyone will move down and make room 831 00:40:12,309 --> 00:40:15,750 for that new one." It's kind of like a waterfall of numbers. It's just kind of like the 832 00:40:15,750 --> 00:40:18,639 movie the Matrix, right, it's kind of like the numbers all just move down, the next 833 00:40:18,639 --> 00:40:19,889 number will take its spot. 834 00:40:19,889 --> 00:40:22,348 So how do I do that? 835 00:40:22,349 --> 00:40:25,459 Star Trek, think Star Trek. 836 00:40:25,458 --> 00:40:28,639 All right. So I'm gonna take all the existing labels that I've already put on 837 00:40:28,639 --> 00:40:30,548 the screen and move them down. 838 00:40:30,548 --> 00:40:34,219 How much am I gonna move them down? I'm gonna move them down in the Y 839 00:40:34,219 --> 00:40:35,940 direction, the height of the label. 840 00:40:35,940 --> 00:40:39,289 So the reason why I first create the label without a location 841 00:40:39,289 --> 00:40:43,469 and set the font is because I want to know how big that 842 00:40:43,469 --> 00:40:44,759 label actually is 843 00:40:44,759 --> 00:40:47,820 and how much I need to move everyone else before I put this on there. 844 00:40:47,820 --> 00:40:51,700 So I say" Label get your height, tell me how big you are so I can move everyone 845 00:40:51,699 --> 00:40:53,639 down that much space to put you in," 846 00:40:53,639 --> 00:40:55,639 and now I'm just gonna have a four loop. 847 00:40:55,639 --> 00:40:58,670 And that four loop is gonna go through my label list, which means it's gonna 848 00:40:58,670 --> 00:41:01,938 go through all the existing label objects I have. 849 00:41:01,938 --> 00:41:05,629 I'm gonna say, "Hey, in that label list get the [inaudible] label 850 00:41:05,630 --> 00:41:08,959 and move it down by the amount DY." Okay. 851 00:41:08,958 --> 00:41:12,978 So this four loop goes through all of the objects that are in my list of 852 00:41:12,978 --> 00:41:13,539 labels 853 00:41:13,539 --> 00:41:16,920 and tells them to all move down, so whatever place they were on the screen 854 00:41:16,920 --> 00:41:19,088 before, they all move down one step, 855 00:41:19,088 --> 00:41:20,349 and that step is 856 00:41:20,349 --> 00:41:21,929 DY pixels. 857 00:41:21,929 --> 00:41:25,318 And after they've all moved down they said, "Hey, hey, we made space for the new guy. New guy, 858 00:41:25,318 --> 00:41:26,509 come on, come on, come, and the new guy runs over 859 00:41:26,509 --> 00:41:29,338 and is, like, ah, I'm part of the number group now." 860 00:41:29,338 --> 00:41:33,159 And so we add him to the canvas. We add the label; 861 00:41:33,159 --> 00:41:37,730 we added the start X and the start Y location, which is always the same. Start X 862 00:41:37,730 --> 00:41:40,119 and start Y are just some consistencies that are given here. 863 00:41:40,119 --> 00:41:44,089 So the new guy always goes to the same place, everyone else just moves down to make 864 00:41:44,090 --> 00:41:44,999 room for it. 865 00:41:44,998 --> 00:41:48,259 And I want to remember, "Hey, new guy, we're not just adding to the screen, 866 00:41:48,259 --> 00:41:51,418 we have to add you to the ArrayList, too." If we don't add you to the ArrayList, we're not 867 00:41:51,418 --> 00:41:53,898 gonna move you next time we need to move you when you're no 868 00:41:53,898 --> 00:41:58,118 longer the new guy. So this add is adding to the canvas, 869 00:41:58,119 --> 00:42:03,278 this add is being called on the labels ArrayList, so it's adding this label 870 00:42:03,278 --> 00:42:07,650 object to the ArrayList. So there's two adds we do. One puts it on the screen; 871 00:42:07,650 --> 00:42:10,139 one puts it in the Array List that we keep track of. 872 00:42:10,139 --> 00:42:13,778 Any questions about this code? Let me just 873 00:42:13,778 --> 00:42:16,280 run this code so I can show you what's actually going on 874 00:42:16,280 --> 00:42:20,039 and how everyone just moved down for the little guy. Okay. 875 00:42:20,039 --> 00:42:22,839 Which I realize is vaguely what happens to your life when you have a 876 00:42:22,838 --> 00:42:25,748 child. Basically, everything else in your life that you thought was important 877 00:42:25,748 --> 00:42:28,338 moves down and the little guy really takes over. 878 00:42:28,338 --> 00:42:29,808 So here's graphic numbers, 879 00:42:29,809 --> 00:42:32,699 and it's just the most wonderful thing in the world. All right. 880 00:42:32,699 --> 00:42:33,969 There is zero. 881 00:42:33,969 --> 00:42:35,389 I clicked once, and 882 00:42:35,389 --> 00:42:36,889 now I'm gonna click again. 883 00:42:36,889 --> 00:42:42,209 One comes in. I'm gonna click again. That means zero's got to move down by DY, 884 00:42:42,208 --> 00:42:44,129 ones got to move down by DY, 885 00:42:44,130 --> 00:42:45,689 and then two comes in. 886 00:42:45,688 --> 00:42:47,519 And if I keep clicking, 887 00:42:47,519 --> 00:42:51,269 everyone just keeps moving down and the new guy - or new woman I should say - 888 00:42:51,269 --> 00:42:53,559 or new it if we want to be gender neutral - 889 00:42:53,559 --> 00:42:56,269 just shows up in the same place, and then you can play games with your friends, 890 00:42:56,269 --> 00:42:56,838 like, 891 00:42:56,838 --> 00:42:58,630 first to a 892 00:42:58,630 --> 00:43:00,559 100. 893 00:43:00,559 --> 00:43:04,630 Yeah, because it's, like, 4:00 A.M. and you're, like, 894 00:43:04,630 --> 00:43:09,309 "No, no, first to 895 00:43:09,309 --> 00:43:12,919 a 1,000." And number zero is still there. It's just way off the screen, but it's 896 00:43:12,918 --> 00:43:16,298 still down going, "Yeah, new guy, come on. I'm just moving for you, right, and it 897 00:43:16,298 --> 00:43:18,838 doesn't know it's not being displayed anymore." Okay. 898 00:43:18,838 --> 00:43:22,119 So any questions about that? All right. So you can have an ArrayList that contains any 899 00:43:22,119 --> 00:43:23,410 kind of object. 900 00:43:23,409 --> 00:43:27,239 Now there's one last thing I just want to show you that's just 901 00:43:27,239 --> 00:43:29,219 fun and cool. Okay. 902 00:43:29,219 --> 00:43:31,749 And here's the thing that's fun and cool. And 903 00:43:31,748 --> 00:43:34,488 the thing that's fun and cool is that now that you've learned about all this stuff with 904 00:43:34,489 --> 00:43:35,920 arrays and we're, like, 905 00:43:35,920 --> 00:43:38,889 "Oh, you can have an array of numbers or you can have 906 00:43:38,889 --> 00:43:41,388 a matrix of numbers," and you kind of look at that and you're, like, "Yeah, 907 00:43:41,389 --> 00:43:45,739 Miron, yeah, that's kind of fun, kind of, sort of, but you know what I 908 00:43:45,739 --> 00:43:50,099 really like, I like pictures. Can I do stuff with pictures in arrays?" 909 00:43:50,099 --> 00:43:51,689 And you think for a while and you say, 910 00:43:51,688 --> 00:43:56,568 "Yeah, because if you couldn't, I wouldn't be asking you that question and setting you up for the fall." 911 00:43:56,568 --> 00:43:59,308 Sometimes I might, but not right now. 912 00:43:59,309 --> 00:44:03,879 Think about a picture, right - and by a picture I mean a G image. 913 00:44:03,878 --> 00:44:06,358 What is a G image, really? A 914 00:44:06,358 --> 00:44:07,489 G image 915 00:44:07,489 --> 00:44:13,539 is basically made up of a bunch of pixels and those pixels live in 916 00:44:13,539 --> 00:44:18,050 an array basically, actually, it's a grid. They live in a two-dimensional array. Right. 917 00:44:18,050 --> 00:44:21,429 So if I have some G image that looks like a smiley face then 918 00:44:21,429 --> 00:44:24,630 maybe I have some pixels 919 00:44:24,630 --> 00:44:28,079 that look like that, and I just have some G image, and those are the individual pixels. And 920 00:44:28,079 --> 00:44:31,690 you look at that and you say, "Hey, that's kind of cool. Could I actually manipulate that 921 00:44:31,690 --> 00:44:33,249 grid of pixels?" 922 00:44:33,248 --> 00:44:34,909 Yes, in fact you can. 923 00:44:34,909 --> 00:44:38,978 And the value that's stored in each one of these elements of the G image - there 924 00:44:38,978 --> 00:44:41,960 are some pixel arrays, which are actually a two-dimensional array, and I'll show you 925 00:44:41,960 --> 00:44:42,940 how to 926 00:44:42,940 --> 00:44:43,950 manipulate it in just a second. 927 00:44:43,949 --> 00:44:45,189 There's an 928 00:44:45,190 --> 00:44:48,628 int that's stored in each one of these cells. You might look at that and say, "An int? 929 00:44:48,628 --> 00:44:51,119 Why an int? I thought I could have all these colors. 930 00:44:51,119 --> 00:44:54,940 Well, it turns out in fact you can encode a whole bunch of different colors 931 00:44:54,940 --> 00:44:59,559 in a single int using something called RGB Values, which stands for 932 00:44:59,559 --> 00:45:00,680 red, 933 00:45:00,679 --> 00:45:01,398 green 934 00:45:01,398 --> 00:45:05,699 and blue. And if you ever look real, real closely on your TV screen, every little 935 00:45:05,699 --> 00:45:08,150 dot on your TV screen, unless you have an LCD 936 00:45:08,150 --> 00:45:11,660 TV screen, if you look at the old school TV screens they're really made up of a little 937 00:45:11,659 --> 00:45:14,750 green dot, a little blue dot and a little red dot, 938 00:45:14,750 --> 00:45:17,679 and the way you get different colors depends on how intense each one of those three 939 00:45:17,679 --> 00:45:18,779 things is. 940 00:45:18,780 --> 00:45:23,280 So this single integer actually encodes all three of these values using an 941 00:45:23,280 --> 00:45:25,870 encoding scheme that's actually described in the book. It's not worth going into all the 942 00:45:25,869 --> 00:45:27,650 details. You don't need to worry about it. 943 00:45:27,650 --> 00:45:31,050 But all you need to know is that you can get that red, green and blue value out of 944 00:45:31,050 --> 00:45:33,459 this single integer. Okay. So 945 00:45:33,458 --> 00:45:34,799 if you can do that, 946 00:45:34,800 --> 00:45:38,829 what kind of stuff can you actually do with your program? 947 00:45:38,829 --> 00:45:40,829 You can take an image, 948 00:45:40,829 --> 00:45:44,459 let's say a color image, and turn it black and white. And you say, 949 00:45:44,458 --> 00:45:47,768 "How do you do that, Miron?" So let me show you. We're gonna 950 00:45:47,768 --> 00:45:51,708 read a G image from a file, so this is just the standard G image stuff that 951 00:45:51,708 --> 00:45:52,649 you've seen before. 952 00:45:52,650 --> 00:45:54,150 We're gonna create a new image 953 00:45:54,150 --> 00:45:57,088 that's gonna read this file called "milkmaid.gif," which is basically 954 00:45:57,088 --> 00:46:01,228 just the little picture that you'll see in just a second of a little milk made. 955 00:46:01,228 --> 00:46:02,619 And then we're gonna create a 956 00:46:02,619 --> 00:46:05,829 grayscale image of it, and then we're gonna display both the regular 957 00:46:05,829 --> 00:46:08,819 image and the grayscale image at sort of twice their size, we're gonna scale them by 958 00:46:08,820 --> 00:46:12,269 two because it's actually a small image to begin with. So we'll scale them just so you can see it more 959 00:46:12,269 --> 00:46:12,849 closely. 960 00:46:12,849 --> 00:46:16,210 And we'll show them side-by-side to each other so I just put 961 00:46:16,210 --> 00:46:20,199 in some parameters to make them side-by-side. How do I create the grayscale version? 962 00:46:20,199 --> 00:46:24,469 What I do is I'm gonna pass that G image, there's a parameter called "image" to a method 963 00:46:24,469 --> 00:46:27,469 called "create grayscale image," which we're just gonna write together or I'm just 964 00:46:27,469 --> 00:46:29,019 gonna show you here. 965 00:46:29,018 --> 00:46:32,828 What I can do from a G image is ask it for its pixel array, 966 00:46:32,829 --> 00:46:36,709 that thing over there, it's a grid; it's a two-dimensional array of integers. So 967 00:46:36,708 --> 00:46:39,038 what I get back 968 00:46:39,039 --> 00:46:40,979 in this value that I'm gonna call an array 969 00:46:40,978 --> 00:46:44,368 is it's basically just a two-dimensional array of integers, 970 00:46:44,369 --> 00:46:48,329 and I'm getting a copy of all of the pixel values of that image. 971 00:46:48,329 --> 00:46:50,500 How do I figure out how high the image is? 972 00:46:50,500 --> 00:46:52,998 I can ask that array for its length because remember a two-dimensional array is an 973 00:46:52,998 --> 00:46:56,929 array 974 00:46:56,929 --> 00:46:58,478 of an arrays, which means that the array itself, 975 00:46:58,478 --> 00:47:00,798 or this thing called image, 976 00:47:00,798 --> 00:47:05,559 is basically an array that contains one row per element. 977 00:47:05,559 --> 00:47:10,579 So what's the height? It's the number of rows I have. So if you ask the main array 978 00:47:10,579 --> 00:47:14,980 itself how many elements do you have, it tells you how many rows it has. 979 00:47:14,980 --> 00:47:16,909 That's the height of the image. 980 00:47:16,909 --> 00:47:18,878 So I get that from array dot length. 981 00:47:18,878 --> 00:47:23,139 How wide is the image? It's how many columns are in one row, so if I say, "Hey, I 982 00:47:23,139 --> 00:47:26,068 can pick any row I want," but I'm just gonna pick the first row because I don't 983 00:47:26,068 --> 00:47:28,929 know how many rows there are and I don't want to pick some row that doesn't exist 984 00:47:28,929 --> 00:47:32,879 so I say, "Hey, first row, you're array sub zero, what's your length?" 985 00:47:32,880 --> 00:47:35,769 And it says, "Hey, I'm 40 pixels across." 986 00:47:35,768 --> 00:47:38,348 And then you know what the width of the image is, too. 987 00:47:38,349 --> 00:47:41,729 Now once I have that here's the funky thing. I'm gonna have this double 988 00:47:41,728 --> 00:47:44,778 four loop that you saw before when I was going through a grid, right, so I have 989 00:47:44,778 --> 00:47:47,079 some index [inaudible], 990 00:47:47,079 --> 00:47:49,470 I'm gonna set my individual pixel value 991 00:47:49,469 --> 00:47:53,529 to be the element that of the array I sub J, so I'm gonna pull out each one of these individual 992 00:47:53,530 --> 00:47:55,599 pixels and integers one at a time. 993 00:47:55,599 --> 00:47:59,739 And there are these three methods in the G that are provided for you, static 994 00:47:59,739 --> 00:48:04,068 methods of the G image class called get red, get blue, and get green, and they get 995 00:48:04,068 --> 00:48:08,599 the corresponding red, green and blue values that are encoded inside this 996 00:48:08,599 --> 00:48:11,180 single integer and they return them to you as integers. So what you get are these 997 00:48:11,179 --> 00:48:14,228 little intensity values for the red, green and blue. 998 00:48:14,228 --> 00:48:18,108 And based on red, green and blue, there's these wonderful folks, the National TV 999 00:48:18,108 --> 00:48:20,759 Standards Consortium, something like that, NCSC, 1000 00:48:20,760 --> 00:48:21,800 who said, "Hey, 1001 00:48:21,800 --> 00:48:24,829 in order to go from a color to a black and white, 1002 00:48:24,829 --> 00:48:29,150 you want to consider how luminous the image is," and luminosity depends on the 1003 00:48:29,150 --> 00:48:29,829 color. 1004 00:48:29,829 --> 00:48:32,338 So it's about .3 times the red, 1005 00:48:32,338 --> 00:48:36,150 the greens a lot brighter so it's almost .6 times the green, and blue we 1006 00:48:36,150 --> 00:48:39,530 consider really dark so it's about .1 times the blue. And these are just 1007 00:48:39,530 --> 00:48:42,048 numbers that got pulled out of the air by this consortium, 1008 00:48:42,048 --> 00:48:43,489 but that's how they determine 1009 00:48:43,489 --> 00:48:47,368 what the luminosity or how bright something is relative to its color. 1010 00:48:47,369 --> 00:48:50,929 Funky, but someone does it. And then we just round that value to an integer because we're returning an integer and that's what we return, and we say, 1011 00:48:50,929 --> 00:48:53,778 "Hey, 1012 00:48:53,778 --> 00:48:54,849 that's great. 1013 00:48:54,849 --> 00:48:59,430 You gave me that luminosity, I'm gonna set that same luminosity value for the 1014 00:48:59,429 --> 00:49:04,759 RG and B images." If you set the RG and B values to all have the same value 1015 00:49:04,760 --> 00:49:06,519 what you get is a grayscale 1016 00:49:06,518 --> 00:49:10,098 because all the color blends out. And essentially you get something that varies from 1017 00:49:10,099 --> 00:49:12,210 completely white to completely black 1018 00:49:12,210 --> 00:49:13,588 depending on how intense 1019 00:49:13,588 --> 00:49:17,608 you've given those RGB values, and I store that back in this array, and at the 1020 00:49:17,608 --> 00:49:21,598 very end I'm gonna create a new G image out of that array and return it. 1021 00:49:21,599 --> 00:49:25,059 So let me just run this for you in our last minus two minutes together so you 1022 00:49:25,059 --> 00:49:29,809 can actually see it. Anyone want to take a final guess as to what I am? Spock. 1023 00:49:29,809 --> 00:49:31,479 Very close. 1024 00:49:31,478 --> 00:49:34,348 I'm almost Spock. [Inaudible] 1025 00:49:34,349 --> 00:49:36,660 I'm Mirror Spock. 1026 00:49:36,659 --> 00:49:39,938 Yeah. Also known as evil Spock. 1027 00:49:39,938 --> 00:49:43,518 And it's because the whole costume is all around the goatee because when my wife saw 1028 00:49:43,518 --> 00:49:47,278 the goatee she's, like, "You're evil Spock," and so our son is actually Spock. So when we sit next to 1029 00:49:47,278 --> 00:49:49,568 each other he's small Spock, he's about this big, 1030 00:49:49,568 --> 00:49:52,958 and I'm evil Spock. 1031 00:49:52,958 --> 00:49:53,918 Yeah. 1032 00:49:53,918 --> 00:49:55,278 It's so cute 1033 00:49:55,278 --> 00:49:57,139 if you're kind of geeky like me. 1034 00:49:57,139 --> 00:49:59,719 All right. So here's the original milkmaid image, 1035 00:49:59,719 --> 00:50:03,328 and here's that same image when you created the grayscale of it. 1036 00:50:03,329 --> 00:50:06,879 So just one last thing. If you shut the camera - because now we're gonna 1037 00:50:06,878 --> 00:50:08,578 get something 1038 00:50:08,579 --> 00:50:10,009 to do if you actually want to see it -