Wait, that doesn't say arrays. This is lists. Yeah, Unit 6 is on arrays. But if you've done block-based programming before, probably array is the one you haven't heard. That's because most block-based languages choose instead to offer lists to people. Lists in Java are not the same as arrays and arrays don't exist in block-based language. By the way lists are not equal to arrays. Here's the difference. When we create an array in Java, which you can think of right now is a list, but the size or the number of elements that we're going to have in it is fixed. So if you remember with in many block-based on what you can add things to them, you can keep adding things and they'll just add more things on the list as needed or you can remove things. But in Java we're going to say, I need a list of 10 integers. Or I need a list of a 100 doubles. That's what we're going to get, and we can't change that. We will always have a 100 spots. So again, the big gene difference is with a list size of the list can't change while the program is running. That's a little bit annoying, but we can actually still leverage a lot of your pre-existing knowledge about lists from block-based languages, to really help you with arrays. By the way, just to preview, we actually have lists in Java too. They are something called ArrayLlist. They're going to be an awful lot like lists that you know of in block-based languages. Even beyond that works, you can have 2D Arrays, two-dimensional arrays. So think of like a grid. We're going to have those in Java as well. So let's start with snap, because if you've taken my other classes, you've probably used a snap. So we've got our pallet up there. We're going to be working in the variables area, and you create a variable, and we make it a variable name, and then to make a variable into a list, you just do that by how you initialize it. So set my list and then from the red blocks down a little bit for the blow, you can pick out that list and you can put the values in the way you want. Then once you've got that, you can do them something that we're going to do a lot of in arrays, and is similar to what we did. Remember what we were doing for loops. We did the find most wanted. Yeah, here's a max, and we can do that on block-based languages. So we've got our array of myList, we have our max variable was at a detour to zero. There's probably a better choice. We're going to have that and you can in snap, although we might not have used it much, there actually is an option for a four i equals something, to something, which now that we know Java, that looks just like a for loop, right? It behaves just like a for loop, except that we have to update i ourselves so that's the thing. But you can see we've got, if max less than item of i of myList, then set max to item i of myList. This is going to look a lot like a Java code. So again, one of the things that's cool if you did do snap, is that you've seen this for loop explicitly before. One of the key words that we're going to need to know about in dealing with arrays is this index. This i value which we'll use, I will use index. You can call it whatever you want, but that's going to be a reference to something that we have. So if we were looking at our code again, we're going to be using that index to get the item out of myList. Here's the actual Java code. Take a look at almost line to line, lines up. So on the four and the Java we do i less than myArray.length, and up here we got to create i2, the length of the list. But other than that, these things look really similar. The ordering or the nomenclature, instead of item i of myList, we say myArray sub i. Those square brackets we read as sub, that's important. So it's my array sub i. However, we're also even going to learn about another type of for loop that we haven't seen before, that we can also use for iterating over lists. Has some limitations, and I will just preview for you that because of that, students hate to use it because, that would mean they'd have to figure out in advance if it was okay for them to use it or if it broke the limitations. So that's okay and again in The big in snap we have this for each item in myList. Then you didn't have to do the item i of myList. You could just talk about item. It automatically just got you each item one at a time. In the same way, we're going to see what we call the enhanced for loop, or a for each and Java, and that's just a slightly different nomenclature. Again, you don't have to index into, you don't have to say myArray sub i. It sticks it into item, and you can choose any variable name you want there and just refer to that. The problem here is that you can't change the value and item inside the loop. If you do, it doesn't actually change the array. That's why students don't make it. All right, what about scratch? What have you done? Scratch is actually a little bit different than snap in this regard, so I'll briefly go through it. Here in scratch you had to make a list directly and put it in. Then it's kind of a paint to add items. I've shown you on the left here, you can use some code to do it. You can manually modify the list in the diagram, which is different than Java certainly. But other than that, a lot of it is really similar except I want to point out the bottom part, but it's smaller, look out here. So let me zoom in. We're not using a for loop here, because scratch doesn't have that for i one to five or whatever it is. The best thing you can get with them is a repeat until the index, which is i, is greater than the length of myList. So we'll start off with index at one, and we'll repeat until the index tends to be greater than the length of myList and we're changing the index by the bottom each time. If you've noticed that on the right we're starting at zero, and on the left we're starting index at one, i zero, index one. Good call. We're going to get to there. It's so important, I'm sticking out at the end for big punch. By the way though, just because we can iterate over loops in Java using the for, doesn't mean we have to. We could use the closest thing that Java has to, and repeat until which has a while loop. We can initialize our variable but beforehand and say while i less than myArray.length, then we can update i by ourselves. So it looks really similar. The order of the arrow is different because repeat until is, if you remember is the opposite a while, repeat until, is like do this while it's false, and once it's true, stop, and of course do it only while it's true. So here's the big punch. The most important thing and it will drive your kids nut. Java indexing of arrays starts at zero. It's one of those silly cultural things that we own as programmers. We say computer scientists start counting at zero. So if you ever see anybody start counting at zero, the rest of the world thinks we're crazy, but it is a sign of being a computer scientist, and it's a little geeky thing that you can share with kids. So just to be really clear about this, on the Snapcode on the left, for i equal 1,2 length of myList because our lift has five things in it. That's going to be 1, 2, 3, 4, and 5. So we'll be getting item one out of list, item two out of list, item three out list, item four out of list and item five out of list. In our code over here in Java, we're going to start off i at zero. So we'll say, i is less than myArray.length. The array length is still five. There's five items in there, so it's going to be 0, 1, 2, 3, 4, and whenever we index, myArray sub i is going to be myArray sub zero, myArray sub one, myArray sub two, myArray sub three, myArray sub four, we go to zero up to the length minus one. So again, the length is five because there are five items, but our indexes are zero up to five minus one or n minus one. There are things that arrays can't do, that list can do. But if you don't remember, I told you you're going to have ArrayList, they'll be able to do these things. These things are really nice and valuable, but they can let kids have to know how to loop over things, etc, just automatically add something to a list, delete something from a list, insert something into a list, replacing item in the list. They can do all of that even in scratch, you complete all of a list, which is nice, but we'll get to those as we move on to our next unit. The one thing I haven't done for you in this block to text video, because it's done so well in CS awesome, is to give you a visual of how we represent array variables when we're tracing code or things like that. But they have such good stuff, like a really cool video and a couple other things. So we'll cover that in 6.1.