 Welcome to Intro to Java with an emphasis on AP Computer Science A with Tokyo EdTech that is me. Our topic today is arrays. Let's see what we're gonna learn. We're gonna learn how to create an array. I'll probably tell you what an array is. We'll learn how to access and modify array elements, how to pre-populate arrays with certain values called initializer lists. We'll talk about a very important topic, maybe again one of the more fundamental most important topics in my opinion in AP Computer Science especially is traversing an array. Traversing any type of collection that comes up time and time and time again. There are three ways we can do that with a for loop, a while loop, or an enhanced for loop. I'll talk a little bit about developing some basic algorithms with arrays. Let's go ahead and get started with the code. So here we go. So creating an array. Well first of all what is an array? And so let me let's say if we had some code like this let's say score 0 equals 34, int score 1 equals, you know, it doesn't matter, let's say 45, int score 2 equals, let's say 57. So let's say, you know, we had three scores. So we have three scores up till now we would need three variables. Now let's say you have a class of a hundred students or some college classes or a thousand students. You have to make a different variable for each of those. However what we can do instead is to create an array which is a group of, you know, similar data types grouped into one organized structure. So let's go ahead and create an array of integers. Now watch what I do here. So int and square brackets. Where have you seen that before? So int square brackets will say scores equals new int. So it's a very similar to structure to what we've seen before but a little bit different here. Now I'm going to go ahead and put a 7 there. And what that does, if I compile that make sure it works, what that does is it creates an array with seven boxes, seven spaces. So if I did something like system, system.out.println scores zero. I'll just put that in the next section. So how we access those boxes is by index. So if you recall from our string unit indexes indices always start at zero. So notice index one. Notice it's printing a value of zero. Okay this is because I created an array of ints and the default value is zero. Again if you look at the linked in ebook down below you can see a nice little graphic for that. So our indices are from zero is the first one and then six would be the last one because we started zero. If I tried to do seven, compile it, I'll get an array index out of bounds, array index out of bounds exception. Okay and we saw that again in the strings unit. So now I can modify values. I can say scores say zero equals, what did I have? 34 was it? Scores one equals, I think I have 45 and scores two equals, what does it say? So now if I try to access by printing I'll see these values one, two and three. Go ahead and compile that and now you can see so that scores zero before we modified it. See that in the output and then we modified it and then we said scores zero, scores one, scores two. We're gonna print those and we get our 34, 45 and 57. So a couple comments about this. Once you have initialized the size of the array you cannot change it. So that's kind of you know it's kind of a bummer. So you really got to make sure you have enough space for all of the data. If you leave out the number it will not compile. You'll get a Java error dimension missing, array dimension missing and a point to where it is. It tells you where the problem is. Java is very nice like that. You can initialize an array with a length of zero. I mean there's no elements can be put into it. So if you try to print or access nothing exists. And you would use that like in the case where you're using like a placeholder for your array and later you assign it to a different one. So yeah be careful with that. So I'm gonna go ahead and put this back to seven and then again there's no reason I chose that was just a random number. Now so that is a case. So I could have done double scores equals new double and then the default value would have been zero point zero. I could have been done Boolean you know scores equals new Boolean and the default value would have been false. So that is a way of creating an array. You create a bunch of empty boxes. There's a default value and then you can you know modify those values as you wish. So there's another way of doing this with what's called an initializer list. So an initializer list would look something like this. So let's say let's do string just don't forget square brackets names equals. Let me put some curly braces here. And let's go ahead and put so then if I go here and I say you know I'm just gonna copy this easier names. So you notice I could put strings there I can put in second but I put any type of object really but I can also put primitives. So you can see gen is zero jr is one and Scott is index two. It's very very cool. Just you know maybe it's kind of obvious but just to make sure you're aware I can do this. I could print scores plus scores two and that would work as well. And you can see it comes out to 102 that's 45 plus 57 102. So there's a lot of things we can do with that. Yeah so we can do all kinds of things. So I think that's pretty good. So you can either create an array with default values. You can create an array with kind of pre-populated values but remember you can't change the size. So in this case there are three items. I cannot add a fourth item. Not change that size. So traversing an array. This is where it gets a bit more I think useful. So we go ahead and do int ages equals and we'll just put a bunch of ages in here. 17, 22, 19, 31 and 27 for example. So what we want to do is we want to iterate through each one of these things. So just like we did with strings it's gonna be four int i equals zero. i is less than. So what we're gonna use here is ages dot length and then i plus plus. Notice there is no parenth, there are no parentheses here. If this was, if this was a string we would use parentheses but it's an array so the length. In this case the length is one, two, three, four, five. So this is index zero, one, two, three, four and the length is five. So what we can do is you go int age equals ages i. You'll see this pattern over and over again. System dot out dot print ln age. Just like we saw it with the strings unit and if I compile this and run it you'll see that all five were printed in the order they are in the, or in the array. We can do the same thing with while loops. I can do int i equals zero while i is less than ages dot length. And I can do, I'm just gonna copy and paste this. Make it easy. And this is a while loop. Don't forget the increment goes here and compile it. See what happens. So you can see we got that twice and 1722. And just as a reminder notice that even though I have int age here and int age here it's okay because this int age is local to this code block and this int age is local to this code block. Now our final one is our enhanced for loop and this we've seen this maybe a little bit before. So for int age colon ages and how I read that in my head is for each age in the array ages for each age in ages and this is why I'm always really picky about variable names. So notice this is plural this is plural this is plural but this is singular. If you keep to that pattern in your life it would be a lot easier. So system dot out dot print ln age. So notice in this case we're not using an index. It just automatically assigns it for us starts at zero and goes all the way to the end. So if I run that, compile it and run it, you'll see it once again I get the same output. So basically there's three different ways to do the same thing. Yeah that's about it. I mean it's very very similar to what we do with strings. So we you know we could do stuff you know we could put some kind of condition if the age is greater than you know 18 so or 20 or let's say greater than or greater than 19 that like 20 and above in Japan then say hey you can vote because your age is over 19 20 is the voting age here in Japan. So there are a lot of different types of standard algorithms. I'm not gonna go over all of them but I will go over one here. Again the e-book has it has a list of them that you know you might find useful things that you might end up having to do. So I'm gonna go ahead and comment this out and I want to use scores again scores scores scores scores scores scores let's go ahead and do that. So let's say we have a list of scores or an array sorry plural equals and say we got 45 85 and 77 good score there 23 not so good there 76 and so what we want to do is we we want to find the maximum value in this in this example. So how to approach a problem like this the first thing is we know that we need to search every single value because the maximum value could be here it could be here could be anywhere in between so we do have to check all of them. This is an unsorted list but we're looking for int max value so what we would do here is we would go ahead and say well we know that it could be here I'm gonna say scores zero and then I need to iterate through this this is what we're looking for looking for then we're gonna iterate through just like we did above so I'm gonna go ahead and use a for loop for int i equals zero i is less than scores dot length i plus plus again this should just be basically ingrained in your brain. So then we're gonna go ahead and get an int score equals scores i and again you could you could skip that step if you know what you're doing and so then what we're gonna do is check so if the current score is greater than the max value then we set the max value equal to the current score hopefully that makes sense we do system dot out dot print ln max value this plus max go ahead and compile that okay so you see variable i is already defined up here I have int i equals zero so I could just take this out for now or I can change it to j whatever whatever is better for you you see here it says max value 97 so just one thing about this so we've got 45 so the max value is 45 so i equals zero score equals scores i which is 45 is 45 greater than 45 no okay we come back around we look at 85 score is 85 is 85 greater than 45 yes it is so we change the max value to 85 come back around we look at 75 is 75 greater than 85 no it's not we come back around is 97 greater than 85 yes it is we swap the value so max value is now 97 we go look at 23 not greater than look at 76 not greater than this is a pretty good solution the only difference I only change I would make here is that we have score zero we know that there's no way score zero can be greater than score zero they must be equal just by definition so to make this just slightly more in slightly more efficient instead of starting at zero here I would start with one again with fast computers not gonna make a difference but just you want to be as efficient as possible we still get the same result and we but we know our code is definitely more efficient now again in the e-book there are multiple different types of algorithms you should try to familiarize yourself with you know in some form or another one or more of them may come up on the AP exam if you are planning to take that and it's also a good practice to get used to figuring these things out this is this is really what computer science is about is you know finding algorithms to do things efficiently that's kind of the core of it so you could do the same thing a good exercise would be to do this with trying to find the minimum value and see if you can change the code to do that you can try it with a for loop or sorry a while loop and try with an enhanced for loop you should be able to do it with all of those pretty efficiently so I think that's it for this unit that's about all there is so we looked at how to create an array like an empty array with default values we looked at accessing and modifying array elements talked about initializer lists which is pre-populating the data traversing an array using for while or enhanced for loops and a little introduction to developing an algorithm so you're kind of what do you look for first you know what are we looking for you know what is the condition where we would change it and just kind of work work your way through that kind of you know in your head a little bit so yeah that's it thanks for watching