Transcript for:
Java Arrays with Zombie Apocalypse Example

whether you're in high school or learning on your own arrays in Java can be kind of hard to understand in this video I'll show you exactly how to use arrays in Java and also how they can help you swap weapons during a zombie apocalypse what's up guys my name is Alex back with another Java tutorial on this channel I make fresh new programs every week to help beginners so if you're new here consider subscribing so we just found out that we're in a zombie apocalypse and in our house we have a shotgun we have an assault rifle and we also have a sniper rifle shotgun only works on close zombies the assault rifle only works on mid-range zombies and our sniper rifle when we shoot those long range zombies and a cool thing about programming is that you can make a program to do that though I'm gonna show you and we're gonna use arrays in Java so to start go to file new Java project and then we'll call it our zombie killer and hit finished next open it up right click the source and go to new class and this will set up our Java file for us and we'll also call this our zombie killer and tick this last this first check mark here and that'll be our last step and hit finished now everything's set up for us to start swapping weapons and blowing up some zombies first we'll just welcome ourselves to this simulation we'll say system dot out dot print line just to print some text to the screen and we'll just say welcome to our zombie killer program just like that and we'll save that and run it and when this runs we'll just see that text pop up on the screen and let's go back to our programming window make it a little bigger and let's start setting up our weapons we have three weapons we have a shotgun we have an assault rifle and we have a sniper now free weapons isn't too bad but what if we had like a hundred that might be a lot to keep track of in our heads and on the program an easy way to keep track of a long list of things is called an array and all it is is a list of whatever you want so this will be our list of zombie weapons each of our weapons is a string which just means a word or a sentence we just type string and then we can name it so this will be like our backpack next do a box like this with the square square brackets and then to add those weapons just type it like this so we have our shotgun and then go the next one do a comma what else do we have in our backpack we have our assault rifle rygel assault rifle and then we have our sniper and it's a pretty big backpack because I don't know of any that can fit all of these and pat yourself on the back now because you just wrote an array it's just a list of whatever you're trying to keep track of we also have our zombies so let's add our zombies like this and put a box and then we add our zombies we have our close range zombie we have our mid-range zombie and we have our long range zombie and if we ran this program now nothing would happen because we're not printing anything to the screen and to bring this to the screen we can just do that with the print statement here since now our program knows that we have three kinds of weapons and there's three kinds of zombies we can start to pull them out and use them so say a close-range zombies attacking which weapon do we want to use well we want to use our shotgun which is the first element in the list so to take it out of our backpack we just type backpack and then the first element now save it and then we'll run it here and see what gets printed out so we pulled something out of our backpack but it's actually the assault rifle I thought if we got the first element in our backpack it should be the shotgun since that was first most programming languages start off with zero and that's called the index if we want to get our shotgun we type in backpack zero if we want our assault rifle we type backpack one and if we want the sniper we type backpack too it's kind of tricky to get used to but you'll get the hang of it just remember to always start at zero and then count from there so let's practice a little bit let's see all the items in our backpack go to backpack items and we'll say get to zero and we'll copy it and get one in two so this will pull out our shotgun then our assault rifle and then our sniper so we'll run it and here we go our backpack items are our shotgun assault rifle and sniper and we can do the exact same thing with our zombies so we can say these are the zombies in the zombies array we'd get zombies of the zero index zombies of the one index and then zombies of the two index save it run it and now here are our zombies close range mid range long range okay so there are like three long range Dom bees right outside my door right now and one closed ring zombie and I really need to kill these as fast as possible because I do not want my family to die so we'll just print something to the screen and what did I say there were there were three long range so to get our sniper what do we have to do we go into our backpack and I remember this indexes in dentistry start at zero and to get our sniper we go zero one two okay so we pull out our sniper and we'll needs to take three sniper shots for three long-range Dom bees okay and then we need to we need to get the closer in zombie right and that was a shotgun so I remember a raise use indexes which start at zero so to get our shotgun we go into our backpack and get number zero so let's save it run it and see if I survived sniper sniper sniper shotgun oh man that would not have been good really glad we had this program to save my life but but like honestly in video games this is how it works when you have five different weapons and you press one through five on your num keys the computer holds it in a list or probably an array and it's grabbing it from that index pretty much this is really cool stuff and you can use other types of variables as well it doesn't have to be strings or words so let's do one with some numbers do int for integer will just say numbers that zombies hate and we'll put them in our box and what do zombies hate they hate the number four in case you didn't know they hate the number 90 they hate the number 70 they hate the number one two three and they hate all these other numbers we'll just make this a little bigger to grab item from here is just the exact same thing say we want to get 561 well we know arrays start at 0 and then we count from there so 0 1 2 3 4 5 6 if I do numb if I go into numbers zombies hate and then put that 6 in there this will output that 561 number so I hope a different perspective like this has helped you learn arrays in Java and please let me know if this was helpful I challenge you to customize your backpack what are some of your favorite weapons in video games I know growing up mine was the spas-12 shotgun from Modern Warfare 2 there was the ACR I was a big fan of and then the l96 a one from called e black ops that was one of my favorite snipers you could even customize your zombies as well you could call one of them a crawler just have some fun with it I'm curious to see what you guys come up with question of the day how did you customize your zombie arrays in Java what challenges did you face connect with me in the comments and engage with this community of new programmers make sure to subscribe so you see all my newest videos the day they come out if this was helpful smash that like button and share it if you think it might help someone you know you could be anywhere in the world but you're here with me and I appreciate that catch you [Music]