• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Stack Class in Java

April 7, 2022 Java

In this post, you will learn how to work with the Stack class so that you can use that same knowledge and thus evolve.

  • this class is basically a stack (due to the way it works) and represents a LIFO (Last In - First Out) type collection, that is, the last value entered will always be the first one out;
  • these values that make up a stack are called elements.
Stack (object).push() Adds an object at the top of the stack
Stack (object).pop() Remove and return the object at the top of the stack
Stack (object).peek() Returns the object at the top of the stack without removing it
package com.caffeinealgorithm.programminginjava;

import java.util.Stack;

public class StackClass {
  private Stack<Integer> stack = new Stack<>();
  private int multiplier = 10, number = 1;

  public void Run() {
    for (int index = 1; index <= 5; index++) {
      stack.push(number);
      number *= multiplier;
    }

    printStack();

    System.out.printf("\nRemoving the number %d from the stack with the pop() function.\n\n", stack.pop());

    printStack();

    System.out.printf("\nThe number at the top of the stack is %d.", stack.peek());
  }

  private void printStack() {
    for (int number : stack)
      System.out.println(number);
  }
}

/*
  1
  10
  100
  1000
  10000

  Removing the number 10000 from the stack with the Pop() function.

  1
  10
  100
  1000

  The number at the top of the stack is 1000.
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved