Understanding Stack in java Libgdx game tutorial




In this tutorial you will learn what is stack collection and how to use them.So first we want to have just the class and Main method for this demo. So remove everything from the main method if you have one or create another class and name it whatever you may call it. So what is a stack actually in java. Well the stack is in some way similar to the list since it can hold elements likely, but there are many differences. Each element is added to the top of the stack each element that we remove is removed from the top. This is also known as a list of collection or in other words The stack is "Last in" "First out" type of collection. you can imagine stack is a file of plates putting plates on top of each other is just a stack of plates. when we need some plate we just take the first on the top which is the last one that was added on top. Stack class defines it method push() to add elements for the stack and pop() method to remove and get an element from the stack, a stack is often used with card games, so it is easy to look at it as a file of cards when dealing cards We always deal from the top of the card, But in some cases, we will need also stack in other games. So it is good to know how it works and how useful it can be. see the code example below.

COMPLETE CODES:
public class StackDemo{
public static void main(String[] args){
Stack<String>card = new Stack<>();
card.push("Ace of diamonds"); //bottom
card.push("Ace of hearts");
card.push("Ace of clubs");
card.push("Ace of spades"); //top

System.out.println("size =" +card.size());
for(String Card: card){
System.out.println("Card =" +card);
}
System.out.println("size =" +card.size());
while(card.size() > 0){
String cardName = card.pop();
System.out.println("dealing card =" +cardName);
}
System.out.println("size =" +card.size());
}
}

inside our "main method"Let's create a stack so stack it also uses generics as a list of "String" as example And now we can push or add our elements to stack. So the first line will be like "card. push", Notice that there is a method push() in a stack. There is also a method pop() and there is a method peek(), peek just gets the element but it doesn't remove. With pop() we are getting and removing elements at the same time. So "card.push," think it we want to put some cards, cards will be represented as String within this lecture. for example card.push("Ace of diamond"); and this will be at bottom of the stack, the Second line of example might be "Ace of heart", third to fourth might be "Ace of the club", "Ace of spades" And this is the top of the stack the "ace of spades" is the last, So it goes on top. Now let's prints the size of our stack or deck using "System.out.println("size =" +card.size());" if we want to print the elements in stack in the same order as they are added to stack, We can use a for-loop statement like this

for(String Card: card ){
System.out.println("Card =" +card);
}

 And now we can again print size by typing

System.out.println("size =" +card.size()); 

Let's run the code and see the output. Let's see how the codes behave. in the output you will see that sizes are four on start before we started printing after printing size is still 4. So nothing was removed from our stack And you can see it brings the first "Ace of diamonds" "Ace of hearts", "Ace of clubs", "Ace of Spades" This is the same order that we are adding the elements to the stack but when we use method "pop()" we are removing and getting the card at the same time. So let's simulate dealing of cards using

"while(card.size()>0){
String cardName = card.pop();
}" 


So that means in other words while there are any cards in it we will get or pop an element from the top. With pop() will pop that element and it gives us the element but also removed. Now when we finish with dealing we also want to check the size of it so type "System.out.println("size = " +card.size());"  This is the same line that we have above just to check that there is something on there. So now when we run the game of our small application we can say, We will see how dealing cards prints are different orders, Why it is printing a different order? will pop always takes a card or element from the top of the stack. Since ace of spades was last, Imagine that you have a real deck of cards "ace of spades", on top. And when we first pop the cards from the top of our deck we get "Ace of Spades" So it is printed first then the ace of clubs, ace of hearts, and ace of diamonds, We can say it removes them but actually they are printed from top to bottom by top to bottom I mean reverse order from the top of the stack to bottom of the stack, size is zero, why the size is zero?. Well because Pop gets the element but also removes the element from the stack. So whenever we pop code from our deck and we deal that card the deck gets smaller and smaller after we deal all the cards there is nothing in our deck. So you can imagine stack is just a file of cards that you can deal with players.



Post a Comment

0 Comments