 Introduction to AP Computer Science Unit 6, arrays or arrays depending on how you want to pronounce it. So this video just let you know is being recorded much, much later than the first five videos. The first five videos I did last summer in preparation for the course and now we've kind of caught up to those videos and now making Unit 6 arrays and arrays. So let's get started. An array, in case you don't know, is just a way of, in my mind, of bundling similar information. So for example, let's say we had int score 1 and equals, let's say, 50. Let's say int score 2 equals 100. Int score 3 equals 75, etc., etc. Now if we only have a few scores, this is perfectly acceptable. But what if we had say 100 scores or a million scores or a larger data set? And what we would do in that case is we would use something called an array. And that's what we're going to be taking a look at today. So section 6.1, array creation and access. So we're going to learn how to create an array and we're going to learn how to access the information in that array. So it's very similar. If you notice what I just did, I had int score equals something. With an array what we need to do is do something like this. We put square brackets, depending on where you're from, you might call them something different. Put square brackets. We give the array a name. Now notice in this case I used scores. I used a plural noun because it's very descriptive of what the information is. Now if I put score, it kind of doesn't make sense, especially later when we do certain things with loops, which you'll see in a few minutes. And then I have to put equals new. And I have to put the type again. So we got int here one time. We got int here another. And in this case I want to put the number 7 there. And the 7 was chosen, just semi-randomly. I have 7 students in my class right now. I have 7 wonderful students. And so I thought I'd use that number. So if you remember back to the string unit, if you watch that video or if you've been studying AP Computer Science, this will be kind of similar to how we accessed strings and the characters in that string. So anyway, so what I've done here is I've created an array of scores. And it is 7 items long. So what I can do now is I can start assigning values. Now just a little thing, I didn't have to use in here. I could have used double. And then I could put double here. Oops, not couple, double. I could have done Boolean. I could have done any of the primitives. And I could also do objects or reference types, whatever you want to call them. So I can also do strings. Now when I create this array, I'm going to go back to int there. When I create the array, there are some default values added. So basically if it's ints, I'll have an array of 7 ints. So I'm going to put that in here, default values. So if it's an int, it is 0. If it is a double, it is 0.0. So I like my formatting consistent. If it is a Boolean, it is going to be false. Now if it is an object or reference or a reference type, I should say, like a string, it will be a reference type. It will become null because we haven't actually defined what it is yet. So you can count on that being 0 or in this case of ints, it will be 0. So what I can do is now I can access each item by its index. So we have 7 items. And just like with strings, the first index is actually going to be 0. So here I'm going to assign some values. And I can say like this. So for example, scores 0 equals, let's say 100. So I did very well. Scores 1 equals 90. So in my array, I'm going to have 0, 1, 2, 3, 4, 5, 6. Not 7 because there's 7 items, but we start at index 0 just as we did with strings. So now what I can do is I can actually print that information out. So I'm going to say system.out.println. And then I'm going to say scores 0. So hopefully you can predict. So this is this. So we can just substitute in 100 basically. And then I'm going to lazy, so I'm going to copy this. Oops. No, not good. And there we go, scores and print scores 1. And what I can also do is I can do things like this, scores 0 plus scores 1. So I'm going to run that. Oops, you see some of the assignment from earlier. So you can see 100, scores 0 is 100. Scores 1 is 90. And then 100 plus 90 is 190. Now just to show you, if I did print out, let's say scores 2 and scores 3, for example, it doesn't matter what order at this point, it should be 0 because that's the default value. Now also just to show you what happens, if I do try to access number 7 or 8 or 9, something outside the range, you're going to see the type of error we get. So exception in thread main, you'll see an array index out of bounds exception. It tells us 7 is wrong. Because it's going to be 7 minus 1 because it's going to be 0, 1, 2, 3, 4, 5 and 6. Let me just put that back the way it was. So that was an array of integers. So that's one thing. Now let's try just for fun. Fun may as a relative term. Let's try an array of strings. It functions the same way. Array of strings. So I'm going to make a new array against string. Now this should look very familiar. You've probably been seeing this all year or since you started selling Java. You never quite knew what it was but now you do. So args is going to be an array of string arguments. And that is passed to the program when you run it. If you're using, say, the command line or calling it through the system thingy. I know a very technical term, system thingy. So I'm going to do some names. And again, new string. And in this case I'm going to do 7. But again, you can do any number here. You can even do 0. But you can't leave it blank because you'll get an error. So you have to put something there. You can have an empty array, although I'm not quite sure why you would do that. But apparently you can do that, which I found quite interesting. So I'm going to do 7. And I'm going to add a few names. And let's say 0. And these are some old friends of mine, not my students. Oops. Names 1 equals gen. And names 2 equals jr. Let's give a shout out to my old peeps. So again, same thing. I can print these out. System.out.println. And names, oops, not putting quotes. Names 0. Oops. Copy that out. And 2. And I'm going to go ahead and just print 3 here. Just to show you what happens. I'm going to show you that it should be null, I believe. So it's got gen, jr. And null because we never said what names 3 was. Again, just a reminder, we're starting at index 0. So this is one way of creating a list. Sorry, list. That's Python. I apologize. I do a lot of Python programming. Anyway, so this is one way of doing an array. So we create the empty array first. We define the size. One of the things with arrays is that once you set the size, it is set. You can't expand it. You can't shrink it because you're setting aside a certain amount of memory. So you need to make sure that you know exactly how much space you need, you know, a maximum case. So you need to think about how you're going to use your memory. Today's computers are pretty, you know, there's tons of memory. It's not such a big deal, but it's something you might want to consider. Yeah, that's one way. Now, in a case where you actually already know what you want to put into the array at the beginning, you may want to pre-populate your array. So for example, so we use something called an array initializer list. So the format is very, very similar. So let's see what we do here. Let's do cities. So I'm going to do string again. And I'm going to say cities equals, actually not new. And I do it like this. I'm going to put some cities in there. Now I live in Tokyo, so I'm going to put Tokyo in there. I've also lived in Seoul. Let's say I lived in Bucharest. And yeah, those are probably the three places, three capital cities I've lived in in my life. And so now ask yourself, how long is this particular list? What is the length of the list? And clearly it's 1, 2, 3. However, the index is 0, 1, 2. So this may be a good spot to figure out how, well, let's print that out real quick. So system.out.println. And I'm going to say cities, new cities, 0. And again, I'm just going to copy that. Cities 1 and cities 2. So again, the thing here is you've got to keep in mind, I've only made room for three cities. So if I need more, I should have used a different method of defining this. Now for something like days of the week, you know there's only going to be seven days of the week. So you can just define that right at the beginning and you know what the answer is. Now real quick, to get the length, the length or the size depending on how you want to put it, is very similar to strings but just a little different to make it complicated. So what I can say here is I can say system.out.println. It's the array name.length. Notice there are no parentheses here. If it's a string, you've got to put parentheses because it's a method. If it's an array, you use the attribute length. So no parentheses. So let's go ahead and run that. And you can see how we got Tokyo, Seoul, and Bucharest. And I'm going to go ahead and actually just to make this a little bit, make the formatting a little bit nicer. I'm going to print out println a blank space. At the end here. Sorry, I should say a blank line after my print statements. Do I have any more? Nope. Okay, and then at the end here, that four. Again, this is just for formatting for my output. Okay, so we've got our integers. We've got our strings. We've got more strings of some cities. Yeah, I've lived it. I do still do live in Tokyo. That's a point in time. It is 2020. Crazy to think we are in the future. So that is the basics of creating and accessing arrays. Now, just one thing. Once you define a value, you can change it. So let's say I make gen here as gen. I say, oh, she decides. Okay, I prefer to be called Jennifer for professional purposes. I can say Jennifer. And then if I print that out, and then we should see the change. Okay, so you can change these values at will. You can't change the type. You can't change the length. But you can do a lot with these. And we'll get to that in a few minutes. Okay, so let's move on to 6.2, which is where it gets quite interesting. And this is why arrays are so useful. This is when we combine them with loops. And this is called traversing arrays. Traversing is just a fancy English word for kind of going across. So you can traverse a bridge, that sort of thing. So what we're going to be doing is we're going to be going through each element, and there's a good look at it, each element in the array. In the case of strings, each element was a letter. Okay, but in the case of an array, it could be an int, it could be a double, it could be some object, which is very, very powerful. Excuse me for taking a drink here. So, let's take a look. We've got a couple different arrays to choose from. We've got our scores, we've got our string names, and our cities. So let's try a couple different methods here. And some of these will look familiar. So let's say for int i equals zero, because it always starts at zero. I think it's something to put that much space in there. Semi-colon. Notice i is going to be less than, we'll do scores for this one, dot length. Notice it's not less than or equal, it's less than, and it's not minus one. Well, if you do equals, it'd be minus one. But in this case, it scores dot length. Again, no parentheses, as I mentioned earlier. And then we're going to increment i plus one. And then we're going to say system, dot system, dot out, dot print. And I'll say scores. And in this case, i. So what should happen is i's going to be zero. I'll print score zero. And then i will be one, scores one, two, all the way up to one minus the length of scores. So if you recall, there were seven scores. And so then I want to print a little space at the end there. Let's try it. So you can see we've got score zero, scores one, scores two, scores three, scores four, scores five, and scores six. That kind of makes it pretty easy. So imagine now that we can, now we want to do something with the scores, calculate an average, find the sum. We can do that even if we have seven scores or if we have 7,000 or 7 million scores. Just one change to the code. Actually, we don't even have to change this code because if we want to go through all of the items, this will work for any size array, which is quite nice. Okay, so we can do the same thing just to show you. The same thing absolutely will work with strings. So there's no reason it has to be integers. So this pattern holds out. And so I go to names. And then down here I'm going to print names. And let's see what we get here. Again, notice how we have zero, one, two, which we defined or we gave a value to, but because it is an object, it is null, null, null, and null because we haven't defined them. So yeah, it's pretty cool. And let's see here. Now, a couple of things. We can also use a while loop. So for example, int j, in this case, equals zero because we want to start at zero. I can say while j is less than names.length. Actually, I'll do cities in this one. Cities, I can just system.out.print cities j. Put a little thing at the end here. Oops, that was not it. Okay, I'm going to run that and compile and run it. Okay, so ooh, now we got a little error. Ooh, I made a mistake there. I did a nice little logic error. This is a while loop. So I'm going to end that. And I forgot to increment j. So I can do j plus plus. This is a while loop out of for loop. Rookie mistake. Okay, so we got Tokyo, Seoul, and Bucharest. Yeah, so that's basically it. I mean, now things like, you know, we could do things like for i equals names.length. In this case, minus one. i is greater than or equal to zero. And we can do i minus minus. And we could do the names backwards. So let me try that real quick. Okay, so we went from six, five, four, three, two, one, zero. And then we can iterate or we can traverse them in the opposite order. Maybe cases where you need to do that. And we'll see later how to actually reverse an array as well. Okay, moving right along. This is a bit of a longer video, but this is kind of important. And it's something that really, really is helpful in simplifying your code and making it very efficient and more maintainable. Okay, so we're on to 6.3. This is called enhanced for loop. And enhanced for loop, if you've taken, if you study other computer languages, you might hear this called a for each loop. And this is just a really, really powerful structure. It has a lot of use cases. Excuse me, I'm drinking some green tea since I'm in Japan. And so what it lets us do is it lets us iterate through a loop without using an index, which is really pretty cool. So for example, we've got our scores. We've got scores. I'm going to say for int score in scores. I'm going to say system.out.println score. Okay, so what this does, this is really cool. So I've made a new variable here, score. So how you would read this to yourself, for each integer score in scores, print that score. So again, I'm going to say that one more time, for each score in scores. That's why I made this plural, make this singular, so it's very, very easy to understand what's going on. Print that score out. So what it will do is it will start at the beginning, score zero, go through. Now, if we do this method, we don't have access to the current index. But in this case, we don't need it, because we just want to print out all the scores. So let's try that. Okay, so you can see, print them out, zero, one, two, three, four, five, and six. So no matter what size that array is, it's going to print them all out. I can do the same thing with four string, for example, and let's do cities, sort of city in cities, cities, and system.out.println city. So this is, you can see, this is very, very powerful. This was an object, we could go through each object and call it a certain method, for example. This is very useful when you're programming games. Let's try that. Okay, so you can see how it went Tokyo, Seoul, and Bucharest. And again, there's not much else to tell about that. It's pretty interesting, but you just notice that the type has to be the same. So scores up here was defined as an array of ints, and so down here, it's got to be, excuse me, it's got to be an int as well. Cities is an array of strings, so we had to make city a string, which is, you know, fair enough, and probably fairly straightforward. Yeah, that's a really, like I said, very powerful structure and very useful at times. But in some cases you can't use it because you do need access to that particular index. Okay, the next section is a little bit long. So I'm going to go through as fast as I can, as quickly as I can. There are some standard algorithms or just standard things that you'll end up doing a lot in computer programming, and there are a number listed in the guide for the AP people, and I'm going to go through most of them. The only one I didn't get was the mode one. I meant to look it up, but I just did not have time. So what I'm going to do is I'm going to go ahead and run through several of them, and then you can watch, and hopefully learn a little bit something. I've been doing the coding bat exercises if they're still available because they're pretty good at helping you through this kind of stuff. Okay, so the first one is determining a maximum or minimum value. So let's say, let's see what we got here. Let's say, do averages. So I'm going to say int averages equals, well, I don't want to use scores again because I already defined it, but it's a way of defining it. I'm going to just throw a bunch of averages in. Let's do E7, E45, E75, let's see, 99, E93, whatever, just pick some random numbers here basically. So I've got an array of averages, and I want to find the maximum or the minimum value. In this case, I'm just going to show you maximum. Minimum works almost exactly the same. So in a case like this to be as efficient as possible, what we want to find at least, we want to find the maximum. So I need to store that information. So I can say maximum just to make it easy. I call it max, whatever you want to call it, it's fine. I like to type things out. And what I'm going to say is we know it's got to be one of these values. It's got to be one of them. So for efficiency's sake, what we would just do is we'd say the maximum equals averages, zero. So we assume, we say, okay, let's say we're going to assume the first value is the highest. Now we've got to prove that. So to do that, we need to iterate through the rest of these and compare this value to this value. So what I'm going to do is I'm going to say int i. Now I don't want to start at zero because I don't want to compare 87 to itself. I could, it's not going to cause a problem, but we want our code to be as efficient as possible. For i equals one, I'm going to say i is less than averages dot length and i plus plus. So I'm going to say if averages i is greater than maximum, then that's our new maximum. Maximum equals averages. Does that make sense? So we assume that 87 is our highest. Then we start at 45. Is 45 greater than 87? Nope. Come back. Is 75 greater than 87? Nope. Is 99 greater than 87? Yes, it is. If so, we say, okay, the new maximum is now 99. So we run through the whole loop. We do have to check them all. Now we can do a system dot system dot out. Dot print l n. The maximum is quote plus x. Period. I do like to do complete sentences and that should give us the maximum. So let's run that and see what happens. Hopefully there's no errors. Okay. So you can see the maximum is 99. It did find that force, which is pretty cool. Now again, you can do the same thing with minimum, except you make this minimum and then this would be less than instead. But we'll stick with maximum for this one. So that was one. Next one is, what do we got here? There were quite a few listed. Okay. Computing a sum or average. Okay. So sum or average. Okay. And this is a pretty straightforward one. Okay. So first we're going to make, I want to make a list of, well, let's do doubles. We'll say doubles, double, double costs equals, and I'm just going to put a bunch of costs in. So let's say 499, 399, you know, 99, 21, 99, and the prices in America are always 99 cents. And let's say $2 for your newspaper subscription. And what we want to find is we want to find the sum and we want to find the average. So I'm going to say double sum. And like right now we don't know what it is. And I'm going to say, I can do it like this, double average equals zero. Actually they're doubles so just to be consistent I'm going to put 0.0. Now what I need to do is I need to go through each of these and add up, add each of these to the sum. And when I'm done I can calculate the average. Now a couple ways to do this, we could do four int i, blah, blah, blah, blah. But here's a question we need to ask ourselves. Are we going through every single item? The answer is yes. Are we going through every single item in order? The answer is yes. If those two things are true, well the second thing is do we need to reference another item from another item? For example, do I need to know that 3.99 comes after 4.99? Does the order matter? And if the answer to that one is no then I can use an enhanced for loop. So I'm going to say double cost, costs. And again if you keep it the English correct it'll be much easier. So for each cost in costs I'm going to say sum plus equals cost, that cost. Now once I have the sum now I can calculate the average. I can say average equals sum divided by cost dot length. Now I could have put here 1, 2, 3, 4, 5 which is fine, this will work but what if I added another thing here I'm going to change the code here. So it's always better to put cost dot length. It'll make your code, you're going to avoid a lot of errors. Any time you can avoid hard coding a value that could possibly change you want to do that. So with sum we always have to start at zero. Now average actually could be anything because we're going to change it here but just logically it makes sense to make it zero. So now I can print this out. Print ln, what we're doing is sum plus sum and I can do system dot out dot print ln we've got here average plus average. Okay let's run it and test it. Now I don't know the answers but I'm hoping we'll get no errors and it should look logical. So the average is 6.792 the sum is 33 and that looks about right. I didn't really check it but yeah I think that's probably going to be correct. I'm going to add a little print ln I could have put the slash n up here but that's fine, let's keep it consistent. Okay so yeah so we've done that one so we've done that one now. Next one is determine if one element has a particular property. See here. So we're trying to find out if an element has a particular property. So I'll write that down and determine if one element has a particular property. So I'm just going to go ahead and use the costs again and so what I want to do is I'm going to say I want to find out if any of these costs are over $10. So actually I'll say over $9.99 that's $10 or higher. So what I need to do is I'm going to make a boolean. Say is over let's see let's say $10 just make it easy $10 and I'm going to assume that it's false because I haven't found one yet. I don't know if one exists but I do want to know if it does exist. So I'm going to set this equal to false and what I'm going to do is I'm going to iterate through I'm going to traverse the array I should probably use that nomenclature and I'm going to see if it meets that criteria. So again same thing do I need to go through every item? Yes. Am I going from start to finish? Yes. Do I need to know which follows which? No. I'm just checking each one as an individual. I'm going to say for cost it's for int sorry this is a double cost in cost. So I can say if the cost is greater than I'll say $9.99 just to make it easy. So as long as I have one I say is over $10 equals true. Now at this point I'm going to try to throw in a break actually I'll leave it without the break for now and I'll throw the break and just see what happens. I haven't actually tested that one yet but I'm assuming it'll work a certain way. Okay do we see anything? Oh we didn't print out the result. So if is over 10 so that's true let's just do that out let's just paste that actually it's technically $9.99 but we'll leave it at that and we can put else no items over $9. Okay so again we're only looking it doesn't matter as long as there's one okay there is an item over $9.99 Now it's always good to test your code so what I could do is I could go up to here and I could change this to $8.99 So it's really good practice to test your code to make sure that it is functioning correctly and you should also test the following case $9.99 in case you made a mistake okay and I might want to do $10.00 just because that's the next value just go ahead and test that okay there is an item over $9.99 alright okay so let's take a look at the next one which is determine if all elements have a property so this is very very similar to this one in the case where you're looking at one element has a property so in this case we're trying to prove that it's true there is an element that has this property but in this case we're trying to prove if all elements have that property so what I'm going to do is I'm going to go to and say boolean is under $10.00 and we're going to assume that it's true we only need to find one false and then we know that it's not true okay so again I'm going to go ahead and do my four because again I'm going through all of them start to finish I don't need to know what order I don't need to know that you know I don't need to know $3.99 follows $4.99 and so I'm going to go ahead and iterate through that the same way and I could do it with an index no problem so four costs in costs and I'm looking for all elements under 10 in this case so what I can say here I'm just looking for one that is over 10 so if cost I'll say is over 10 and then I'm going to say is under 10 equals false I only need to find one that's not under 10 so again the same thing I can do is if is under 10 nope system.out.print ln and we'll say all items under 10 10.00 that's a pretty simple one and else either all under 10 or they're not else system.out.print ln all items not under 10 so again we need to test this of course I'm going to test that in a second I missed a little something in the last one not the major but just a little tidbit so I'm going to run it and see what happens it says all items under 10 and it printed that twice which is not quite that's why I do that I wanted to especially as the video gets longer I tend to make more and more mistakes so if we look at this where's our cost set so are all items under 10 you can see here the answer is no so let's check our code ah so greater than 10 so I should make that greater than or equal to 10 because we're only looking for one this is a good thing about testing you need to test those edge cases they're called so I'm going to go back up to here I'm going to change this to 999 again so we have a case where we have one that's exactly 10 we have a case where they're all under 10 all under 10 and I'm going to go ahead and make this 999 and go ahead and see what we get out of that one ok so we are getting the proper response it's very important to check your code beginners like to assume oh I think I programmed it right they check one case oh it's working they don't check all the cases and then they have problems later the next one is access all consecutive pairs of elements I'm not really sure what that meant so I'm making my best guess here ah two pairs of elements ok so what I could do is I make some strings an array of strings I should say called foods and I populated that ahead of time with I just went with apple banana really original cucumber I didn't know anything but start with a D egg can't have eggs without ham yeah so we'll go with that so I've got a string of I got an array of strings I should say and now what I want to do is I want to access consecutive pairs so for example I want to print apple and banana together and then I can either do banana and cucumber and cucumber and egg or I can do apple and banana then cucumber and egg actually there's an uneven number so maybe that's not possible so in this case I do need to use a normal for loop so for int I equals zero I is less than foods I'm going to put my .length length I'm going to put minus one here you'll see why in a second so what I want to do here is I'm going to say system.out println I'm going to say foods I plus I'm going to put a little dash here plus foods I plus one so you can see the reason instead of putting length here which would go all the way to ham I need to stop one early because egg plus one if I do ham plus one I'm going to get an array out of bounds exception let's run that and see what happens apple banana, banana, cucumber, cucumber, egg egg and ham and yeah, that one is that again I think that's what that means but I'm not 100% sure but it's a good thing to practice anyway just so you don't go over the end of the array and there's some questions there's multiple choice questions that make you have to look at this and make sure you don't do that okay in the interest of just kind of saving a little bit of time and just kind of getting through this I've gone ahead and I've typed all the code in I'm just going to go through and kind of explain how everything works real quickly and this next one is determining the presence or absence of duplicate elements so basically what we have to do is we have to start with the first element so in this case it's the Hulk and then we have to compare the Hulk to every other element after that and see if they're equal if they're equal then we have a duplicate element okay so and then once we're done checking all the Hulk ones we move on to the flash and then we check everything from flash onwards to see if it's a duplicate we don't need to check backwards from flash because we've already checked Hulk and flash going forward so watch what we do here as we start our first loop here and we go from zero all the way to the end and then we go the internal loop we start from here plus one so that's why I got k equals i plus one and we go all the way to the end and we check if they are equal or not and then once it comes around when we're on flash so i is one k is going to be two and we go all the way to the end and then so on and so forth okay so let's give it a shot and see what happens you'll see some other output from the other ones but that's okay so you can see here are there duplicates false because there are no duplicates now let's say we get rid of the Daredevil Hulk in there again and so we go ahead and run it and we should see true okay so we do have some duplicates okay next one is determining the number of elements that meet a certain criteria so what I've done is I've created an array of strings genders m and f representing male and female and so what we want to do is we want to determine the number so we haven't started yet we don't know what the number is so we're going to set that at zero so let's say we're looking at the number of female board members in this case and so we iterate through the entire loop or through the entire array excuse me and we check and see if the gender in this case each element or item however you want to put it is equal to the condition which in this case is f for female and if it is we increment number of females we complete the loop and then we print out the result now I could have used an enhanced for loop here and the reason is that I want to search every element and the order I search them doesn't matter but this one works as well so I'm going to go ahead and run that and we should get one two and three because there are three female board members now let's go ahead and change this condition well it's not changed let's change this to f and just see if we get an extra female okay so now we have four female board members so it's good to see some good gender representation there okay moving on shifting array elements in this case we're going to be shifting left but you can use the same pattern for shifting right basically so we've got an array of letters they're strings but letters s h a r k and we want to shift them to the left so the s is going to be at the end a r k and s so the problem with this is or at least the way I'm going to do it is I'm going to start at index one and then I'm going to say this equals this okay then I'm going to go up one I'm going to say that this equals this I'm going to say that this equals this this equals this and then I'm done but what happens is because the first thing I did was I changed this s to an h I've lost the s so that's why I have here this string temp letter equals letter zero so I'm going to be saving this before I do anything and then I'm going to iterate through from the first index not zero index but from index one to the end shifting everything over to minus one I could have done it a little bit differently but that's the way I did it here is fine as well and then at the very end once I'm done with the loop I put the temp letter which was the zero at the new end I changed that last letter and then here I'm just printing everything out and then you should see h, a, r, k and s and I think you already see it down below but it's always nice to check that okay and then the last thing is reversing the order of elements so you can see here I've got some names and this actually came from one of the multiple choice questions in the reviews and it's pretty interesting actually so we could have done it a longer way but basically what we're going to be doing is we're going to be iterating from the zero element to the middle element in this case it's conon that's why we have length divided by two because this is an integer instead of being 2.5 or 3.5 whatever it is it's going to go down to 3 which is what we want and then so what I do is I store a temporary variable like we did in the previous one and then I say that this equals the length which should be plus 1, minus 1 and minus i since i is zero I'm going to say that this equals this so varshani overwrites n and then inside the loop this time then I say that last element is now temp student name basically swapping this one and the last one and then swap this one and then this one and then finally it's done because we're not going to get up that far because we went to half instead of going all the way through and what that should do is reverse the string or reverse the array of strings I apologize so we have everything in reverse order so I know it was a little bit quick on the last few you know go back watch the video you can kind of see how it works you have the code and hopefully that will become clear to you thanks so much