Java Convert Array to Stack Example

Java Array Examples


This example demonstrates how to push an array of elements into a Stack in Java.
package com.javaguides.corejava.api.util;

import java.util.Stack;

/**
 * Class demonstrates the usage of Stack class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class ArrayToStackExample {

    public static void main(String[] args) {
        convertArrayToStack();
    }

    private static void convertArrayToStack() {
        String[] strArr = {
            "Java",
            "JEE",
            "C",
            "C++",
            "Spring",
            "Hibernate"
        };
        Stack < String > stack = new Stack < > ();
        for (String string: strArr) {
            stack.push(string);
        }
        System.out.println("Non-Empty stack : " + stack);
    }
}
Output:
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]

Reference

https://www.javaguides.net/2018/12/java-stack-class-methods-with-examples.html

Comments