Stack Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I've got a stack Java tutorial for you by the end of this video you're gonna learn exactly how to use the stack in Java all the methods how it works and everything but let me just take five seconds here to show you my new office check it out I got my very own office space before I shared my bedroom and also my office but now I've got my own dedicated YouTube video space here if you're new here my name is Alex I make Java tutorials just like this one every single week so if you might be interested in that then please consider subscribing but first let's start off this stack Java tutorial by just making a new project we do this at the beginning of every video I'll call it like stacks and stuff hit finish and then on source go to new class I'll just cut like stacks or fun hit this first checkbox for the public static void main and it finished just gonna zoom out a little here I'm gonna get something to ask you what is this and you say Alex will that's that's three video games we got Super Monkey Ball on the top Guitar Hero 2 in the middle and called duty ghosts on the bottom now what I want you to do is just think of one of these games Super Monkey Ball Guitar Hero or Call of Duty now I'm gonna play the game that you chose though say that your game was Guitar Hero I'd have to take off Super Monkey Ball and now I have Guitar Hero if your game was called duty ghosts' then I'd have to take off Super Monkey Ball and Guitar Hero and then I'd be a Call of Duty ghosts and that's how stacks and Java work just like a stack of video games you have to go one at a time from the top we'll just simulate this real real life example stack of video games into Java so to make a stack in Java just type stack don't call it like games and say equals new stack this is red underlines that just means that you know this program doesn't know what a stack is yet we have to bring the stack code into our program and there's a super easy way to do that just hover over it and click import stack that generates this import statement at the top and now we can use this stack now since we got rid of those red underlines now there's the yellow underlines so many underlines but it's just a really simple thing to do here I mean hover over it it just gives us a little message here at the top but it's just asking what type are you gonna put in here is it integers is it strings is it doubles as it floats as Long's the characters ours we're going to represent video games as strings so we'll type little alligators here and put put this string inside the alligators I'm gonna also do that right here now our stacks all set up and we just got to add our games to add to a stack in Java just type the name of the stack and then a period and this period brings up everything that the stack can do for you and there's one called add so I'll just type add and I'll put in our first game now this is a good question what game goes first do we put Super Monkey Ball first or call duty ghosts' first the answer is call duty ghosts' so you want to do ghosts then Guitar Hero then Monkey Ball to get the order we have so call duty will add Guitar Hero and then lastly we'll add Super Monkey Ball who here played Super Monkey Ball as a kid this was the family game we played along with Mario Party if we print out our game so far print out this stack games let's see what happens we get call of duty Guitar Hero and Super Monkey Ball however it's pretty difficult to visualize this because it's going sideways here but the real kicker to the stack is that you should be looking at it like it's not side-to-side so just ignore how it printed out on the screen let's say we want to take off Super Monkey Ball I'll just put it in the same print statement here we want to get the very top of the stack so to do things with your stack you do dot and to get the first one it's called pop to get the very top item so if we print that out then we'll get Super Monkey Ball if we print out the stack now then you'll see that it only contains called Diddy guitar here now because we popped it off and removed it and this is what's remaining if we want to take off guitar here are two then we'd have to do another pop and we got both of those games now if we want to get call duty then we'll have to do yet another pop there are times where you want to look at the very top but not pop it up so keep the stack as is but just look at the very top and that's called peak so if we do games that peak that will print out Super Monkey Ball but leave the stack full so if we run it we get Super Monkey Ball but the stack still has all three I'll show some more methods now with another example say we have a stack of three Legos ones red ones yellow and ones blue and we'll go from top to bottom so our physical example would have yellow on the top blue in the middle and red at the bottom to turn this into a stack or put these in a stack let me just type stack call it Tower there's a new stack here these are going to be maybe say character these these as character variables if there's only one letter to add these to a stack you just type the name dot add will add a red one first because remember you add from the top down read the bottom next we'll add our blue B and next yellow we printed on a Carnot tower then we'll get our b1 just like before we can do a peak peak to look at the top that's yellow we can do a pop to remove the top so the stack now would just be red blue but you can also do some really great methods here with stacks so I'll put a dot and see what else we can do we can see if it contains a certain value so if it contains say a P for purple it returns false since there isn't one in there but if this was say blue then it would return true with stacks in Java you can also do empty this will return true if it's empty and false if it's full so we get false since it's not empty you can also access these elements by index so if I wanted to get B for example I could do get and then put index one in here that will get B if I do a zero then that'll get red so indexes go from bottom to top but when you're peeking and popping those take from the top I think that's where the big confusion is the ordering but if you just picture it like this then you'll be golden you can also set certain indexes so say I wanted to replace blue with something else I know that blue is that index one and I can replace it with P footprint the stack now then the tower is going to look like our py the last thing I want to show you is just you can get the size usually the size or length methods are really good in general and stack has one as well let me know if this video game visualization was helpful for you with stacks you add one by one and then you remove one by one I hope you enjoyed this stack Java tutorial and I'll see you in the next video have a great rest to your weakest [Music]
Info
Channel: Alex Lee
Views: 64,292
Rating: 4.9632182 out of 5
Keywords: stack java, stack java tutorial, stacks in java, stacks java, java stack, java stacks, stacks in java tutorial, learn java, alex lee java, java stacks tutorial, stack in java, how to use stack in java
Id: ILJgewz5Dxw
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Thu Jul 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.