Programming Methodology-Lecture17

Instructor (Mehran Sahami): All right. So welcome back to yet another fun filled exciting day of cs106a. A couple quick announcements before we start. First of which, there is one handout, which is kind of a quick reference for you on ArrayLists. Itís sort of all the ArrayLists you need to know for the hangman assignment part three. If you havenít already done it, now you have a quickie reference for it. If youíve already done it, you donít need the quickie reference, but presumably, you saw everything you needed already in the textbook or what we did last time in class. The midterms, it was just a rocking and rolling good time. If you werenít there [inaudible], but two things, if you took the exam, please donít talk to other people about the exam because there are still some people left to take the alternate exam; and if youíre taking the alternate exam, you missed out. Youíll still take the alternate exam, so youíll get the exam, you just wonít get the full effect of the exam under earthquake conditions, but thanks for actually just Ė it was amazing, it was just like how many people want to continue and everyone was, like, rock on. So thanks for continuing with taking the exam. Iím glad you made it through just fine. Last thing, a little contest. Iím glad to see there was other people besides us Ė we were a little worried it was gonna be like me and 300 normally dressed people, this is not what I normally dress in around the house, just when Iím in meetings on campus. So the real question is who am I? Anyone know? Itís a very specific person. Not King Arthur. What? Student: Sir Lancelot.

Instructor (Mehran Sahami): Not Lancelot. Student: [Inaudible]

Instructor (Mehran Sahami): I almost considered Karel. I almost went for Karel. I got the big box, but itís kind of hard to draw, sort of like, Karel cannot draw that much in the big box. No. Well, Iíll give you hints as we kind of go along, and at the very end Iíll show you if you canít figure it out. I almost considered giving whoever could figure it out a 100 on the midterm, but I figured that was kind of just a [inaudible] way of giving you a 100 on the midterm, so Iíll just give you lots of candy. So anyway, just think about it, and if you want to try to search the web or whatever, it is a one particular character, and itís not a wizard. I actually thought, ďOh, I could just make it easy for you and just come in and be, like, oh, ha, ha,Ē because most people, without the hat, would see it and theyíd go, ďOh, youíre a wizard, right?Ē No. So I just brought the hat to kind of fool you. All right. So with that said, time to actually wrap up one of our last topics, which was arrays. Weíll look at a few more advanced things we can do with the arrays and then get into the details of our next great topic, which is our friend the ArrayList. So when we talked about arrays, a couple people last time said, ďHey, Miron, can I have multiĖdimensional arrays, can I have arrays that have more than one index, and in fact, you can. We do refer to these things as multi-dimensional arrays. And so one way you might want to do it is letís say you wanted to have this dreaded thing called a matrix. How many people know what a matrix is? Iím not talking about the bad movie. Actually, it was a good movie. The sequels, not so great.

Itís basically just a grid of information. Itís a grid mostly of numbers if you want to think about it in a mathematical sense, but really we think of this thing as a two-dimensional array. And so the way you can think about is if I have a matrix thatís a [2] [3] matrix what that means is itís a grid that has two rows and three columns. Okay. And you might look at that and say, ďHey, that almost likes an array, it looks like two arrays stacked on top of each other,Ē and in fact, you would be correct. If we have multi-dimensional arrays, what weíre actually creating our arrays of arrays. So really what we want to do here, if we want to create this thing called a matrix, is we specify the type that weíre gonna store in all of the boxes in that grid, and then we specify as many pairs of square brackets as we have dimensions of that thing. So if we have two dimensions here like we have rows and columns then we specify two open and closed brackets in a row, if we had three like we wanted to make a little thing that represented a cube weíd have three sets of brackets. So here Iím just having two because Iím just gonna show you two arrays so you can think about the generalization from there. And then we give it a name so we might call this thing a matrix. And then we specify basically the creation of that matrix in the same way we created an array except now we have more than one dimension, so here we say new, we specify the type again, like, indices and then to give it the size rather than just having one size that we specify, we have a size for every dimension. So if this things gonna be [2] [3], we specify the first dimension as two and then the second dimension as three. And each one of these is its own pair of square brackets. We donít use comma notations, so if youíve worked with some other language or you say 2, 3, nuh uh. It doesnít work in java. Itís [2] [3] no spaces immediately after it. And so what this does when you create that is it sort of creates this thing thatís this grid thatís a [2] [3] grid. And now you can refer to the individual elements of that grid, so the way you can think about it is this is element [0] [0] or really you want to think of them in brackets, and then this becomes element [0] [1] and this is element [0] [2] and so forth, so this is element [1] [0] and this is [1] [1] and this is [1] [2]. So if you kind of think about assigning to elements of that you could say something like matrix [0] [1] = 5 and what that will do is itíll say, ďOh, where is [0] [1], here is [0] [1].Ē

Well, what Iím gonna do is, in that particular cell in your grid, is just gonna stick in the value five. And you can use that like an integer anywhere else you could use it like an integer or a single element of an array. Okay. The other thing thatís kind of funky about this is that this two-dimensional array is really Ė the way you can think of it is itís an array of arrays, right? Itís an array Ė think of it this way as an array. It has two entries in it and what is each one of those entries of the array, well, really every entry of the array you can think of this as one cell, that whole thing, and this whole thing as another cell and each entry of that first dimension of the array is just another array with three elements in it. Okay? So what that allows you to do is say something like matrix subzero and matrix subzero, the type of this thing, is an integer array. A single dimensional array because what is matrix subzero, itís this full line here. Itís the first row of the entire matrix, and then if I specify something else, like I specify a column, thatís how I get [0] [1]. First I specify the row, and then I specify the column. If I donít specify a column this is referring to the whole row, which means itís just referring to a whole array.

Student: Can you refer to [inaudible]

Instructor (Mehran Sahami): You canít. You have to refer to things in order, so thereís no way to just pull out a column, so if youíre feeling sort of like, ďOh, but in my Math 53 class we do all this stuff with columns.Ē Yeah. You just have to pick, in some sense, whatís gonna be your major ordering for this thing and have that one be first. So you could kind of think of it in your head that, ďOh, well, my first index is really my columns and think of this thing as being the transposed and all that,Ē but you just have to keep track of it in your head. Okay? Any questions about that? All right. So we can write a little code that makes use of this just so you see a quickie example. And we could, for example, go through all the elements of this grid and set our initial values to be one, so we could have something like I = 0. ďIĒ is less than Ė what you want ďIĒ to be less than in the first index is 2. I ++ and then youíre gonna have some other four loop. Letís say this is J = 0, J is less than 3 because thatís the size of my second index, J++. And then inside here I could just say something like matrix I sub I, sub J = 1. And that would initialize all the cells in my grid to be one. Okay? Is there any questions about that? Hopefully fairly straightforward kind of stuff. You can have as many dimensions of arrays as you want sort of within reason, and within reason being how much memory your computer has right because if you imagine, ďHey, I donít only want a [2] [3] matrix, I want a [2] [3] bi-hundred matrix,Ē so this becomes a three dimensional array. Right? So I basically have this grid repeated a hundred times going forward. Suddenly, youíre using up a whole bunch of memory. Right? So you want to be careful doing that, but if you wanted to do that you would just add another pair of brackets here and youíd add another pair of brackets over here with a hundred and now you would have a three dimensional array. So if you want to create, like, your friend the Rubikís Cube or something, with is [3] [3] [3], now you know how. Okay? And if you want to do real funky things with four and five dimensional objects or whatever, be my guest. Okay? But weíre not gonna use them a whole bunch in this class, so I might show you some examples of two ďDĒ stuff momentarily, but most of the arrays weíre gonna deal with are just what we refer to as one dimensional arrays, right, which is just a list. All right? So any questions about multi-dimensional arrays? If youíre feeling okay with multi-dimensional arrays nod your head. Good times. All right.

So moving on from the array we can now go back to our friend that I briefly introduced last time, and now we can go into excruciating detail, our friend the ArrayList. Okay? And the ArrayList Ė the way you can think about is itís an array just kind of cooler. Okay? So the idea behind an ArrayList is an ArrayList first of all really is an object, and itís an object thatís provided for you in the Java Util Package. We talked a little about this last time. Iím just repeating it just for good measure. So you import the Java Util thatís star packet to be able to get at the ArrayList class. Okay? And the way you can think about an ArrayList is itís really like an array, except itís an array that dynamically changes its size depending on how you add elements to it. So you can just say, ďHey, add another element to the end,Ē and it will just go, ďHey, you know what, Iíll just create extra elements at the end for you and now your size is one greater.Ē And so the way to think about it is in terms of effective and actual size, that we talked about last time, was with regular arrays you set up with the actual sizes thatís declared, and then your effective size is how much of it that youíre using. With an ArrayList, think of it as the effective size and the actual size, at least stylistically, are exactly the same thing. Whenever you want to add another element to the end, its effective and actual size both grow. Okay? Now underneath the hood, the implementation actually involves some stuff where the effective and actual size can be different, but thatís all abstracted way for you. As far as youíre concerned, effective and actual sizes are basically the same thing. Okay? And it provides some other functionality that is nice, and I will show you that in just a moment. But this particular ArrayList thing is called a template. Iíll give you a little history of templates. I mentioned this briefly last time, but I will mention it again. Starting in Java, the book refers to it as Java 5.0. A lot of programmers will say Java 5.0. Some programmers will say Java 1.5, and what you have installed on your computer is either Java 1.5 or Java 1.6, which some programmers would say is either Java 5 or Java 6, which you would look at and say, ďHey, man, that makes no sense at all, why is it like that?Ē

So Iíll give you a little bit of history. Quick history lesson for the day. Many years ago, oh about 1995, there was this thing called Java 1.0. It came out, people were happy and went, ďOh, nice,Ē and then Java 1.1 came out and people looked at that and said, ďYeah, nice,Ē and then Java 1.2 came out and people said, ďNice!.Ē So when they said, ďNice!,Ē 1.2 in some peopleís mind was such a major change in the language Java that they started calling it 2.0, and at that point when 1.3 comes out you canít say, ďOh, well, thatís 2.1,Ē right, because then it gets really confusing so then when 1.3 came out people called that Java 3.0. And 1.4 was 4.0, 1.5 became 5.0, 1.6 became 6.0, but in fact, the confusion still exists so if you go and look in your system most of you on your system it will actually have this 1.X number. Most programmers will actually refer to it by just the X as opposed to the 1.X. Donít be confused. Theyíre really referring to the same thing. Okay? So what weíre dealing with here, in terms of ArrayLists as templates, is Java 5.0 or later so sometimes thatís just written as 5.0 plus, which all of you should have because thatís what you installed in the beginning of class. The book actually shows both the pre-5.0 and the post 5.0 version. Weíre just gonna deal with the post 5.0 because at this point, the older version is just not worth worrying about. Okay? So what is this thing called a template that exists in Java 5.0 and later versions? Okay. What a template is is itís basically a class that allows you to specialize it based on the type. What does that mean? Okay. So let me give you a few examples of what that actually means. What that means is when you see methods for this ArrayList class, and youíll see them listed in the book, youíll see something that looks like this. Thereís a method called Add and it returns a bullion and youíre like, ďOh, thatís all good and well,Ē what kind of parameter does Add take and youíll see the spunky thing that looks like this, [T] element, and you look at that and say, ďOh, Miron, you never told me about the [T] type, what type is that?Ē And the way you can think about it is this is a parameterized type. When you create an ArrayList you say, ďIím gonna create an ArrayList of a particular type.Ē So when you set up your ArrayList you create an ArrayList object. And when you create that object, as you saw last time very briefly, in angle brackets, which are actually less than and great than signs, you specify a type like string. So you say ArrayList string and we call this thing, for example, SList and we might create that to be a new ArrayList. Again, we specify the type string () because weíre actually calling the constructor which takes no arguments. Okay? So you create this new ArrayList and this thing that you specify here, the type that you specify there, is what we refer to as the type or the template.

You can think of the template as having all these holes in it, and the way those holes get filled in is they get filled in with whatever type you give it here. And those holes are specified by this [T]. The way you can think of this T is itís the Type, thatís why itís a T that this template is what we refer to instantiated as. You create one of these templates of this type. So I create an ArrayList of strings is the way programmers refer to it. Usually, you can think of this as meaning of and this as meaning nothing. So ArrayList of string is how we would actually say it. And when you create an ArrayList of string you get an Add method for this ArrayList of strings thatís now [T] is replaced by string. So the method that it actually provides for you is an ad method that takes a string type. Okay? If I were to create an ArrayList of G ovals I would have an Add method for that class thatís elements was a G oval. Okay? So it creates for you basically a whole class that has all the holes filled in with whatever type you specify here. Any questions about that? Kind of a critical concept, new in Java 5.0. Itís new, itís dynamic, itís cool, itís funky. People like it, now you can, too. All right. So let me show you some of the methods that you get with this ArrayList kind of thing, and then weíll actually go into the details of using it. And youíll notice that when we talk about the methods, this [T] thing will show up all over the place. And the way you want to think about that is this is just getting replaced with whatever type you instantiate your ArrayList to be. So if we go to the computer real quick, drum roll please, itís the methods of the ArrayList. So what are the methods of the ArrayList? The first two methods are Add. What Add does is it takes in some element type of whatever type your ArrayList is instantiated to be, right, the [T] and it adds it to the end of the ArrayList. And you might say, ďHey, Miron, what does that return a bouillon,Ē and it always returns true for the bouillon, so why does it return a bouillon at all? And about in a week itíll be clear why it always returns a bouillon. Okay? And why, in this case, itís always true, but as far as youíre concerned you donít really care about the return type. Youíre not gonna do anything with it because itís always true. Youíre just gonna say Add and even though itís returning something to you, youíre not gonna [inaudible]. It just adds an element to the end of the ArrayList, which means the ArrayList dynamically grows by one. Right? Itís an array with dynamic size, so it just creates the space for you and adds the new elements to the end. You can also add in a particular position just like you could add an array at a particular position, you can do that in an array list by specifying the index and the element that you want to enter at that index, and it will just put that element in at that index for you and itís just kind of all abstracted in terms of how it does that. Okay? Itís just growing its size as needed. Besides adding things you can remove from the ArrayList, right, which is something thatís funky. You didnít really think about removing from an array. Right? An array you could sort of say, ďYeah, read that value and maybe I donít care about the value anymore,Ē but you couldnít actually say, ďYeah, just get rid of that value completely, like, get rid of the space associated with that value.Ē Okay? So when you say remove at a particular index, it actually removes that element and returns it to you from the ArrayList.

And similarly, you can either remove or you give it an index and you say, ďGive me back that particular element,Ē or you can remove a particular element if it appears. So this returns a bouillon. So letís say you have an ArrayList of strings and one of your strings may be cs106a, and youíre like, ďOh, cs106a, it was so near and dear to my heart until I had to take a midterm in the middle of an earthquake,Ē remove cs106a. And then you try to do this for everyone on campus. Well, some people have 106a and if it appears itíll return true and remove it from the ArrayList, but some people donít. And it doesnít suddenly crash their program or whatever. When you come to that person and you call remove on cs106a, it just returns false to indicate, ďHey, cs106a wasnít in this ArrayList, I didnít make any changes to the ArrayList.Ē Okay. And similarly, if you just get really angry or you decide, ďOh, Iím gonna take some time off from Standard, I want to just clear my ArrayList of classes,Ē you can call ďclearĒ which just removes everything in the ArrayList. It just kind of resets it to be a blank ArrayList. Okay? You can ask an ArrayList for its size, not particularly exciting, but very important to actually be able to do. It just returns how many elements are currently in the ArrayList. Besides putting elements in an ArrayList and removing them it wouldnít be much fun if you couldnít actually get an element, so getting an element at a particular index returns the object at that specified index. It doesnít remove it from the ArrayList, it just gives it back to you. So if there was an object or some element at position one, okay, and you call ďGetĒ it remains at position one. If you call ďremoveĒ itís actually removed from the ArrayList. Okay. And then thereís set, and so set you can actually say, ďHey, at some particular location I donít care whatís there, just slam this thing Iím gonna give you.Ē So what it does is you give it a particular index and you give it some particular value, and it puts that value at that index. So you can say, ďSet at position one, you know, this string 106b because now after youíve taken 106a, maybe next quarter youíre taking 106b, and it goes, ďThatís great,Ē and what it gives you back is the old value at that index. So it would give you back 106a if that was the old value that was there. Just in case you want to do sort of the quickie slight of hand change, presto change, stick something new in, get the old value out. You can do that if you want. Thatís just the way it is. Last but not least, a couple of things real quickly. You can get index of. This is kind of funky. This essentially does a search for you on the ArrayList. So you give it some particular value, like, you could say, ďHey, is 106a actually in my ArrayList,Ē so you would pass it to string 106a and it would return to you the index of the first occurrence of cs106a if it was in your ArrayList and minus one otherwise. Itís just kind of like the index of operation that you did on strings before when you called it with like [inaudible] or substrings.

Itís the same thing here except now you have a whole list of things, and that whole list of things can be any type that you want. Contains, the same kind of thing. It basically just returns a bouillon. You give it some value, and it says, ďTrue or false, this value actually appeared in the list and is empty.Ē It just returns true or false to let you know if the ArrayList contains any elements at all. Okay? So fairly generic, basic functionality and itís all kind of good and well and weíre happy. So letís use some of that and actually write a program that makes use of an ArrayList together so we can see how some of this functionality actually looks, and then weíll try it out on the computer and try a few more advanced things with it. So hereís our friend the Run Method. We got public, void, run; inside here weíre gonna create a new ArrayList of strings. So I have ArrayList of string, ignore that part, although you need it for the syntax, but we just donít say it as programmers. We say ArrayList of strings. My string list, Iím just calling it SList, is a new, very similar to what you saw there, ArrayList that holds strings and thereís no parameters for the constructor, so I make sure to have the () even though it looks kind of funky. You have to have it. So I just create one of these, and now what Iím gonna do is just repeatedly read strings from the user until the user enters a blank line to indicate, ďHey, thereís no more lines that I want to give you,Ē and then Iím gonna write them all out. So I have some wild loop in here and Iím gonna do my friend the wild true loop, right, the old loop and a half construct where Iím gonna have some string line that Iím gonna read in from the user. So Iím just gonna use read line and to keep the prompt short, itíll just be a question mark. And Iíll just assume the user knows to enter a line there. I want to check to see if that line actually contains anything so I check is the line dot equals to the empty string ďĒ and if it is then Iím gonna break, which means the user just gave me a blank line so there are no more lines that they want to enter. And if they gave me a line that is not equal to the empty string, then I actually got some valid line. I want to stick that string inside my ArrayList at the end and tell my ArrayList to just kind of grow in size. So I do that by just saying SList dot Add, and youíll notice the parameter that Addís gonna take up there is something of whatever the type of the ArrayList is. So in this case, itís going to be a string so I can do an Add on line whose type is string and Iím perfectly fine. And this will return to me true, but I donít care because I know it always returns true. So Iím just not even gonna assign whatever turns to many variables. And this will keep reading strings from the user until the user gives me a blank line to indicate that theyíre done, and then Iíll write a little loop so this is just a continuation of the program to actually print all those strings on the screen so you can see what that looks like. Okay? So I have a little four loop over here for I = Ė forgot the [inaudible] = 0. ďIĒ is less than Ė I need to know how many strings are actually entered into my ArrayList. Okay. Well, I can use my methods over there to figure that out with size. So I just say SList dot size I ++, and inside here Iím just going to print line and the line that Iím going to print is sequentially I want to go through my ArrayList and get strings one at a time at the given index. So how do I do that? I just say string list, and whatís the method I use, Ė [inaudible] the single syllable gets candy. And so did everyone else. Mostly the empty seats Ė get ďI.Ē

So that will get the ďIĒ string. I got my parens lined up there and that will print everything out. So it just gets a bunch of lines and the user prints out those lines on the screen. Okay? But notice I donít need to declare an initial size for the ArrayList. I donít need to worry about is it big enough, did I over write the end of it, do I get the big mean evil exception going off the end of the ArrayList? No, it just keeps expanding space as long as I need space. Thatís why itís so versatile to use and when Iím done, I can just go ahead and say, ďHey, whatís your size now?Ē And then I can add more if I want, so let me show you a generalization of this program that we just wrote. So if we come back to our friend Ė all done with you power point. Thanks for playing, and donít save. All right. So we come over to our friend eclipse, and hereís a simple ArrayList example oddly enough named simple ArrayList dot Java. So what this is gonna do is gonna pass around the ArrayList as a parameter doing the same functionality, but I just want to show you what it looks like when we de-compose the program a little bit and how we pass ArrayLists as parameters. So I create an ArrayList of strings here, the same syntax that I had before. I just called it List instead of SList, and I want to read in that list from the user so Iím going to pass as a parameter the whole ArrayList.

The way I pass the whole Array is just by specifying the name of the parameter, which in this case, is list. So Iím gonna read a list from the user. So what does read list do? Hereís the whole read list function. Now hereís the funky thing. If you can get to the computer that would be extremely useful. The read list function Ė how do I pass an ArrayList as a parameter? I just say ArrayList and List and you might look at this and say, ďOh, but Miron, youíre not specifying what kind of ArrayList it is. Is it an ArrayList of strings, an ArrayList of integers,Ē the function or the method doesnít care. All it cares about is youíre giving me an ArrayList, and Iím gonna assume youíre gonna do sort of the right thing with it in terms of putting stuff in and taking stuff out of the ArrayList. But youíre just giving me an ArrayList, so you do not specify as a parameter the type that that ArrayList stores. It knows because the actual object you pass underneath the hood knows, ďHey, Iím an ArrayList of strings.Ē So if you try to stick something in it that isnít a string, itíll get upset. But it just trusts you when you actually pass it as a parameter. So here we say, ďWhile true, string item =s read line,Ē so here Iím just calling it item instead of line. I read a line from the user. If that line happens to be equal to ďĒ Iím done and I break out of the loop. Otherwise, what Iím going to do is add the item that was just read, which is the string, to the list. So itís exactly the same code that I had here, I just have decomposed it out into a function. Now notice I donít return anything from this method, and the reason why I donít return anything from the method is an ArrayList, at the end of the day, is an object.

When I pass an object what happens to changes I make to the object? They persist, right? Itís an object. Itís the like the Mona Lisa thatís being shipped around in the truck. If I go and slice the truck in half, I slice the Mona Lisa in half because I really have the real object. And so I donít need to return anything because any changes I make to this ArrayList, because the ArrayList itself is an object, those changes actually persist. And so whether or not you think of an ArrayList as an object or as an array, in both cases if I pass an array to a method or I pass an object to a method, in both cases the changes persist, so it doesnít matter how you think of an ArrayList. If itís easier for you to think of it as just an array, think of it as an array and the changes persist, and if itís easier for you to think of an object, which is what it really is, then changes persist as well. And the truth underneath the hood is to be real quiet. Array is just a regular ray are objects too. Just so you know. But you donít need to worry about that. All right. Just think of them as this thing called an array. So that reads a list. And then after we read the list from the user, and all these changes that we made to list persist, I print out the ArrayList, and Iíll print out ArrayList. Okay. And Iíll print out an ArrayList again, it gets the ArrayList as a parameter and it just says the list contains list dot size elements, so it tells me how many elements are currently in the list and then it does the little four loop that I just drew up on the board going through all the elements from zero up to size, actually size minus one, and writing them all out on the screen. And Iím gonna do this twice so Iím gonna read the list, print it out and then Iím gonna read to the same list just to show you that the list is gonna continue to grow when I do that, so let me run this. I already compiled it. So itís asking for lines, cs106a. Thatís over in the psych department. CS106a rocks. And thatís the end. It says, ďList contains two elements, cs106a and rocks,Ē and Iím, like, ďOh, yeah, I forgot to add heavily. And now the list contains four elements because I continued to add stuff to that same list. It just continues to append onto the end. Okay? Are there any questions about that? Itís not like you just add and after you add and you stop adding or itís just frozen, itís not frozen. You can just keep adding more stuff if you want. Okay? Any questions about that? Are we feeling okay about the ArrayList?

Now hereís the funky thing. At this point, you should be going, ďHey, ArrayList, kind of cool. How come this time, Miron, youíve been doing ArrayLists with strings? I thought the simple type was like ints. Why werenít we doing ArrayLists of ints? Any guesses? No guesses? No guesses. All right. Iíll give you a hint. Itís a popular science fiction TV show from the 70s. Actually, 60s, I would imagine, 60s and 70s. I think itís actually 60s. Keep thinking. Itíll get you closer. Hereís the funky things about ArrayLists. An ArrayList, as a template, can only be instantiated, which means its type can only be specified to be objects. So ArrayLists hold objects. As we talked about in the past [inaudible] double bouillon and are friend the little care always comes at the end, just kind of struggling along, are not objects. Theyíre primitive types. So I cannot in fact have an ArrayList of ints. If I try to say ArrayList [ints] I get an error because an ints is not an object. So in order to get around that Ė remember a few lecturers ago we talked about, ďHey, for everyone of the primitive types thereís an equivalence class type, so these are the primitives and the equivalent class types, which are called wrapper classes because they kind of wrap around these. The wrapper classes are INTEGER, W, BOUILLON and then character all written out are the wrapper classes. So if I actually want to have an ArrayList that holds integers, it canít hold ints. I need to create an ArrayList that holds integers. It actually has to hold objects so it has to hold instances of some class. Okay. Now, in the bad old days it was Java, you know, pre-Java 5.0 this made ArrayLists really cumbersome to use because every time you had some integer somewhere you said, ďHey, you know what, I have this thing called int X and maybe itís value I said it originally to be five, and now I want to stick this inside some ArrayList of integers that I created somewhere and I canít do that. So I need to actually go and create a new integer object and assign the value for that integer object to be what X is and it just became a huge pain. So you actually need to do something like this. You need to say, ďInteger Y = new integer,Ē and then give it the value X because what you need to do is create a new instance of the integer class and its initial value you gave it as an int, and then it created this nice little box for you which was the integer object and inside the box there is this little space for the actual value and it held the five in there. And that was a pain to have to do this every time you wanted to put some object into an ArrayList of integers. And similarly, if you wanted to get something out you had to do something equally cumbersome, which if I wanted to get the value out. So if I wanted to have some integer Z here and I want to say, ďHey, get the value of that integer, I canít just say Z = Y, at least I couldnít in the bad old days.Ē I needed to actually say Y dot ints value and call a method on it. And then what this gave me back from that object Y was its actual value of an integer, so it would give me back the five as an integer. Okay. All that changed in Java 5.0. So the process of essentially creating an object around a particular primitive, right, so we had our little primitive int five over here, and what this was basically doing was creating Y to be an object around the five.

This process is still called ďboxingĒ because basically what you were doing was drawing another box around your value. And this process over here of going from the box that you had to getting the value out of the box, interestingly enough, was called unboxing because basically when you wanted to get the value out, you cared about the value inside and essentially you wanted to erase the box that was around it. Okay. This whole boxing and unboxing happens for you automatically in Java 5.0 and later, which means if I create an ArrayList of integers I can just add an int to it directly. So if I were to have an ArrayList it still needs to hold object of the integer class, so I still canít say an ArrayList of ints, and Iíll call this IList = new ArrayList integer, so I create one of these things. Now I can say IList dot add and give it X directly where here I might have int X = five, and you would say, ďBut Miron, you told me this ArrayList can only store integer objects and youíre passing an ints. You told me that doesnít work.Ē Well, this is what Java 5.0 is doing for you automatically. It says, ďHey, this things expecting an integer and youíre giving me an int,Ē which means you really need to box it up. Itís kind of like you finished your eating and you still have some food left and you want to store that food somewhere, what do you need to do? Itís got to go in a box. And before you used to have to ask like the waiter or the waitress to bring you a box and they would create it in the backroom somewhere, probably out of other food that wasnít eaten, and then you would get the box and youíd put your value in, you could finally go and store it at home. And now Java just says, ďHey, you know what, people were asking for the box so often, yeah, if you need that food stored in a box, Iíll just put the box on it for you, and then when you want to get it out over here,Ē like you want to say, ďint is Z = IList dot [inaudible], the zeros elements.Ē Before you would just get the box, right, because thatís all this thing is storing are boxes that hold integers. Now Iíll actually take the box out for you and let you eat the food automatically. Okay. So it does that for you automatically, which makes these things a lot more useful. And the thing to keep in mind is that all these wrappers classes are still immutable objects. Theyíre immutable in the same sense that strings are immutable. When you create one of these guys, like this guy Y has the value five, its value is five. You canít go back and say, ďHey, set its value now to be six,Ē because it does not have a method to set its value. It can get you its value by asking for its integer value, but there is no way to set its value. If you now want to create some new integer thatís value is six, then you need to create a new integer and actually say whatís its value, its Y + 1. Okay. Just in the same with strings you couldnít change a string in place. If you wanted to actually change the string, what you really needed to do was create a new string that copied over all characters from the old string that you cared about and potentially made some other changes. Okay.

So the wrapper classes are immutable in the same way strings are immutable. Once you set their values, it does not change. If you want to make it look like its really changing what you do is you create a new one and you would assign it to overwrite the value of the old one. Okay. So let me show you an example with that. And weíll use our same little code here and this is gonna be so much fun all weíre gonna do is say, ďHey, you know what, rather than having an ArrayList of strings, Iím gonna have an ArrayList of integers.Ē And youíre gonna say, ďBut Miron, this is gonna make your whole program blow up.Ē Well, read list doesnít care what type of ArrayList is getting passed in here. Itís just getting passed an ArrayList. I donít want to put strings in there so Iím gonna change my item to be an int and Iíll read in ints from the user, and basically the way Iím gonna stop is if that int is == to zero, and Add actually will just remain the same. And hereís the coolest part of all. Prints ArrayList doesnít change at all 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 in. The list contains the size method doesnít care what type is actually in the list, this list outside doesnít care what type is in the list. And when I get the element from the list, what Iím getting is an integer object, and when I go to print it out, it automatically gets unboxed for me to get turned into an integer and printed out. So even though I changed from an ArrayList of strings to an ArrayList of integers because I never specify the type here and thatís just the way life is. That function will just work on change. Okay. So let me run this program again. Yes, go ahead and save and do all the happy things you need to do. One, two, three Ė [inaudible] numeric format, right, it wasnít in line and it wanted a zero. Now the list contains three elements and then Iíll add four and five and then give it a zero and now the list contains five elements. Okay. Again, the beauty of object drawing and programming and the beauty of templates. Some things can just be reused even though you change the type, and you just need to be careful what youíre actually storing and not storing. Okay. So any questions about that? All right. Another quick example. When we talked about arrays I told you, ďHey, you know what, you can have an array that contains objects in it too.Ē You can have an array of G objects or an array of G ovals or whatever you want, and thatís perfectly fine. And you can do the same thing with ArrayLists. As a matter of a fact, with an ArrayList you donít need to worry about all this boxing/unboxing stuff because if you have an ArrayList of letís say G ovals or G [inaudible] or whatever the case may be.

Those are already objects so thereís no boxing or unboxing that needs to go on. Okay. So letís say I was gonna have an array of G labels, I could say G label Ė actually, let me show you this first of all in just the case of regular arrays and then weíll move on. So I can have an array of labels. So here is labels and I say something like new G label, it has four items in it and what I get here is four elements of my array that all start off no because none of them have been initialized. Okay. Thatís what I got in the array world. If in the ArrayList world I create an ArrayList of G labels I would say something like ArrayList thatís going to store G label, and Iíll call this my G list or my GL = new ArrayList of (G label). This creates for me an empty list of G label objects. If I now want to add G labels I still need to create the actual underlying G label object and then add it to this ArrayList, much in the same way that with a regular list that when I created the array, all I got was just an array of no. So if I wanted to have the individual elements, I still needed to call a new on a G label for each one of the elements that I wanted to create. Same kind of idea going on with ArrayLists. All I get is an empty list and every time I want to add a new G label I shouldíve created that G label object already. Okay. So let me show you an example of this with another funky program we call graphic numbers, or I call it graphic numbers. I just wrote it this morning, and all this thing is gonna do is listen for the mouse. Okay. All of the activities are going on when I click the mouse. So whatís it gonna do? What itís gonna do when the moue is clicked Ė well, before the mouse is clicked I have some instance variable in here. The instance variable I have in here is this thing that I just showed you. I have an ArrayList, itís gonna be private, so itís gonna be within my object, itís an ArrayList called labels that holds G labels. So what I get when this line executes, when the program starts running, is I get an empty ArrayList of G labels. Thereís no G labels in it. The way Iím gonna add G labels is every time Ė this is the whole program Ė every time the user clicks the mouse Iím gonna create a new G label and add it to the list. Okay. So what I do when the user clicks the mouse is Iím gonna create a new G label called ďlabĒ and the label or the text thatís actually associated with this label is just the pounds sign or the number sign and then however many labels are currently in my list, which means at first itís gonna be zero so itís gonna give me a zero and the labels gonna be number zero, and the next time itís gonna be number one, and the next time itís gonna number two, etcetera. But the first time itíll be number zero.

I set the fonts on that label to be Courier 18 because as the years are passing my eyes are having problems so I need the font to be big. And then what happens Ė hereís the funky thing that I want to do. I want to say, ďYou know what, where am I gonna put this on the screen,Ē I always want to put the brand new label, like, when itís born in the same place on the screen, but the problem is if I put number zero there and then when Iím ready to put number one, if I put it right on top of number zero itís gonna get real ugly and itís gonna be a mess, itís like dogs and cats sleeping together, and the whole thing is just not gonna work out. So what am I gonna do? Iím gonna say, ďHey, why donít I take all the other numbers I had before and move them down one line so the new guy will always show up on top, and then when thereís gonna be a new one, everyone will move down and make room for that new one.Ē Itís kind of like a waterfall of numbers. Itís just kind of like the movie the Matrix, right, itís kind of like the numbers all just move down, the next number will take its spot. So how do I do that? Star Trek, think Star Trek. All right. So Iím gonna take all the existing labels that Iíve already put on the screen and move them down. How much am I gonna move them down? Iím gonna move them down in the Y direction, the height of the label. So the reason why I first create the label without a location and set the font is because I want to know how big that label actually is and how much I need to move everyone else before I put this on there. So I sayĒ Label get your height, tell me how big you are so I can move everyone down that much space to put you in,Ē and now Iím just gonna have a four loop. And that four loop is gonna go through my label list, which means itís gonna go through all the existing label objects I have. Iím gonna say, ďHey, in that label list get the [inaudible] label and move it down by the amount DY.Ē Okay. So this four loop goes through all of the objects that are in my list of labels and tells them to all move down, so whatever place they were on the screen before, they all move down one step, and that step is DY pixels. And after theyíve all moved down they said, ďHey, hey, we made space for the new guy. New guy, come on, come on, come, and the new guy runs over and is, like, ah, Iím part of the number group now.Ē And so we add him to the canvas. We add the label; we added the start X and the start Y location, which is always the same. Start X and start Y are just some consistencies that are given here. So the new guy always goes to the same place, everyone else just moves down to make room for it. And I want to remember, ďHey, new guy, weíre not just adding to the screen, we have to add you to the ArrayList, too.Ē If we donít add you to the ArrayList, weíre not gonna move you next time we need to move you when youíre no longer the new guy. So this add is adding to the canvas, this add is being called on the labels ArrayList, so itís adding this label object to the ArrayList.

So thereís two adds we do. One puts it on the screen; one puts it in the Array List that we keep track of. Any questions about this code? Let me just run this code so I can show you whatís actually going on and how everyone just moved down for the little guy. Okay. Which I realize is vaguely what happens to your life when you have a child. Basically, everything else in your life that you thought was important moves down and the little guy really takes over. So hereís graphic numbers, and itís just the most wonderful thing in the world. All right. There is zero. I clicked once, and now Iím gonna click again. One comes in. Iím gonna click again. That means zeroís got to move down by DY, ones got to move down by DY, and then two comes in. And if I keep clicking, everyone just keeps moving down and the new guy Ė or new woman I should say Ė or new it if we want to be gender neutral Ė just shows up in the same place, and then you can play games with your friends, like, first to a 100. Yeah, because itís, like, 4:00 A.M. and youíre, like, ďNo, no, first to a 1,000.Ē And number zero is still there. Itís just way off the screen, but itís still down going, ďYeah, new guy, come on. Iím just moving for you, right, and it doesnít know itís not being displayed anymore.Ē Okay. So any questions about that? All right. So you can have an ArrayList that contains any kind of object. Now thereís one last thing I just want to show you thatís just fun and cool. Okay. And hereís the thing thatís fun and cool. And the thing thatís fun and cool is that now that youíve learned about all this stuff with arrays and weíre, like, ďOh, you can have an array of numbers or you can have a matrix of numbers,Ē and you kind of look at that and youíre, like, ďYeah, Miron, yeah, thatís kind of fun, kind of, sort of, but you know what I really like, I like pictures. Can I do stuff with pictures in arrays?Ē And you think for a while and you say, ďYeah, because if you couldnít, I wouldnít be asking you that question and setting you up for the fall.Ē Sometimes I might, but not right now.

Think about a picture, right Ė and by a picture I mean a G image. What is a G image, really? A G image is basically made up of a bunch of pixels and those pixels live in an array basically, actually, itís a grid. They live in a two-dimensional array. Right. So if I have some G image that looks like a smiley face then maybe I have some pixels that look like that, and I just have some G image, and those are the individual pixels. And you look at that and you say, ďHey, thatís kind of cool. Could I actually manipulate that grid of pixels?Ē Yes, in fact you can. And the value thatís stored in each one of these elements of the G image Ė there are some pixel arrays, which are actually a two-dimensional array, and Iíll show you how to manipulate it in just a second. Thereís an int thatís stored in each one of these cells. You might look at that and say, ďAn int? Why an int? I thought I could have all these colors. Well, it turns out in fact you can encode a whole bunch of different colors in a single int using something called RGB Values, which stands for red, green and blue. And if you ever look real, real closely on your TV screen, every little dot on your TV screen, unless you have an LCD TV screen, if you look at the old school TV screens theyíre really made up of a little green dot, a little blue dot and a little red dot, and the way you get different colors depends on how intense each one of those three things is. So this single integer actually encodes all three of these values using an encoding scheme thatís actually described in the book. Itís not worth going into all the details. You donít need to worry about it. But all you need to know is that you can get that red, green and blue value out of this single integer. Okay. So if you can do that, what kind of stuff can you actually do with your program? You can take an image, letís say a color image, and turn it black and white. And you say, ďHow do you do that, Miron?Ē So let me show you. Weíre gonna read a G image from a file, so this is just the standard G image stuff that youíve seen before.

Weíre gonna create a new image thatís gonna read this file called ďmilkmaid.gif,Ē which is basically just the little picture that youíll see in just a second of a little milk made. And then weíre gonna create a grayscale image of it, and then weíre gonna display both the regular image and the grayscale image at sort of twice their size, weíre gonna scale them by two because itís actually a small image to begin with. So weíll scale them just so you can see it more closely. And weíll show them side-by-side to each other so I just put in some parameters to make them side-by-side. How do I create the grayscale version? What I do is Iím gonna pass that G image, thereís a parameter called ďimageĒ to a method called ďcreate grayscale image,Ē which weíre just gonna write together or Iím just gonna show you here. What I can do from a G image is ask it for its pixel array, that thing over there, itís a grid; itís a two-dimensional array of integers. So what I get back in this value that Iím gonna call an array is itís basically just a two-dimensional array of integers, and Iím getting a copy of all of the pixel values of that image. How do I figure out how high the image is? I can ask that array for its length because remember a two-dimensional array is an array of an arrays, which means that the array itself, or this thing called image, is basically an array that contains one row per element. So whatís the height? Itís the number of rows I have. So if you ask the main array itself how many elements do you have, it tells you how many rows it has. Thatís the height of the image. So I get that from array dot length. How wide is the image? Itís how many columns are in one row, so if I say, ďHey, I can pick any row I want,Ē but Iím just gonna pick the first row because I donít know how many rows there are and I donít want to pick some row that doesnít exist so I say, ďHey, first row, youíre array sub zero, whatís your length?Ē And it says, ďHey, Iím 40 pixels across.Ē And then you know what the width of the image is, too. Now once I have that hereís the funky thing. Iím gonna have this double four loop that you saw before when I was going through a grid, right, so I have some index [inaudible], Iím gonna set my individual pixel value to be the element that of the array I sub J, so Iím gonna pull out each one of these individual pixels and integers one at a time. And there are these three methods in the G that are provided for you, static methods of the G image class called get red, get blue, and get green, and they get the corresponding red, green and blue values that are encoded inside this single integer and they return them to you as integers.

So what you get are these little intensity values for the red, green and blue. And based on red, green and blue, thereís these wonderful folks, the National TV Standards Consortium, something like that, NCSC, who said, ďHey, in order to go from a color to a black and white, you want to consider how luminous the image is,Ē and luminosity depends on the color. So itís about .3 times the red, the greens a lot brighter so itís almost .6 times the green, and blue we consider really dark so itís about .1 times the blue. And these are just numbers that got pulled out of the air by this consortium, but thatís how they determine what the luminosity or how bright something is relative to its color. 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, ďHey, thatís great. You gave me that luminosity, Iím gonna set that same luminosity value for the RG and B images.Ē If you set the RG and B values to all have the same value what you get is a grayscale because all the color blends out. And essentially you get something that varies from completely white to completely black depending on how intense youíve given those RGB values, and I store that back in this array, and at the very end Iím gonna create a new G image out of that array and return it. So let me just run this for you in our last minus two minutes together so you can actually see it. Anyone want to take a final guess as to what I am?

Student: Spock.

Instructor (Mehran Sahami): Very close. Iím almost Spock. Student: [Inaudible]

Instructor (Mehran Sahami): Iím Mirror Spock. Yeah. Also known as evil Spock. And itís because the whole costume is all around the goatee because when my wife saw the goatee sheís, like, ďYouíre evil Spock,Ē and so our son is actually Spock. So when we sit next to each other heís small Spock, heís about this big, and Iím evil Spock. Yeah. Itís so cute if youíre kind of geeky like me. All right. So hereís the original milkmaid image, and hereís that same image when you created the grayscale of it. So just one last thing. If you shut the camera Ė because now weíre gonna get something to do if you actually want to see it Ė

[End of Audio]

Duration: 52 minutes