Stack subclasses Vector which already supports this, try this...

stack.toArray(array)

Here is the Javadoc for this.

Answer from Todd on Stack Overflow
🌐
Techieclues
techieclues.com › blogs › converting-a-stack-to-an-array-in-java
Converting a Stack to an Array in Java
July 3, 2023 - Here, we create a Stack of integers, add elements to it, and then convert it into an array using iteration. We create a new array of the same type as the stack, with a size equal to the stack's size.
🌐
Tabnine
tabnine.com › home page › code › java › java.util.stack
java.util.Stack.toArray java code examples | Tabnine
private static String[] credit_card_number( String[] prefixList, int length, int howMany ) { Stack<String> result = new Stack<String>(); for ( int i = 0; i < howMany; i++ ) { int randomArrayIndex = (int) Math.floor( Math.random() * prefixList.length ); String ccnumber = prefixList[randomArrayIndex]; result.push( completed_number( ccnumber, length ) ); } return result.toArray( new String[result.size()] ); } ... /** * 获取唯一一条最短路径,当然最短路径可能不只一条 * @return */ public Integer[] getBestPath() { assert (vertexCount > 2); Stack<Integer> stack = new Stack<Integ
🌐
GeeksforGeeks
geeksforgeeks.org › java › stack-toarrayt-method-in-java-with-example
Stack toArray(T[]) method in Java with Example - GeeksforGeeks
July 11, 2025 - Program 1: When array is of the size of Stack ... // Java code to illustrate toArray(arr[]) import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method to add elements into the Stack stack.add("Welcome"); stack.add("To"); stack.add("Geeks"); stack.add("For"); stack.add("Geeks"); // Displaying the Stack System.out.println("The Stack: " + stack); // Creating the array and using toArray() String[] arr = new String[5]; arr = stack.toArray(arr); // Displaying arr System.out.println("The arr[] is:"); for (int j = 0; j < arr.length; j++) System.out.println(arr[j]); } }
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 185870 › one-method-for-stack-of-either-integer-or-double-parameters
java - One method for stack of either Integer or ... | DaniWeb
I think that if you use Number class instead of T, you can call methods such as doubleValue() and intValue(), then use the + operator and finally return either an Integer or Double. Number is an abstract class that both of these classes extend, so the true polymorphic method of doing what is required would be to use a Stack<Number> and return Number.
🌐
Decadental
ftp.decadental.com › blog › convert-stack-to-int-array-in-java-a-quick-guide-1767646622
Convert Stack To Int Array In Java: A Quick Guide
January 5, 2026 - The most straightforward method to convert a Stack to an int array is by using a loop in combination with the pop() method. This approach involves repeatedly popping elements from the Stack and assigning them to the corresponding index in the array.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › stack-toarray-method-in-java-with-example
Stack toArray() method in Java with Example - GeeksforGeeks
December 24, 2018 - // Java code to illustrate toArray() import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method to add elements into the Stack stack.add("Welcome"); stack.add("To"); stack.add("Geeks"); stack.add("For"); stack.add("Geeks"); // Displaying the Stack System.out.println("The Stack: " + stack); // Creating the array and using toArray() Object[] arr = stack.toArray(); System.out.println("The array is:"); for (int j = 0; j < arr.length; j++) System.out.println(arr[j]); } } ... // Java
🌐
YouTube
youtube.com › watch
Stack Implementation Using an Array Java / Stack Using an Array Java - YouTube
Java has a Stack class that holds elements of type Object. However, many languages do not provide stack types, so it is useful to be able to define your own....
Published   February 14, 2023
🌐
University of Hawaii
www2.hawaii.edu › ~esb › 2010spring.ics211 › IntArrayStack.java.html
IntArrayStack.java
/* no-arguments default constructor creates an empty stack */ ... if (array.length == top + 1) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; }
🌐
Medium
medium.com › @Harshit_Raj_14 › stack-data-structure-implementation-in-java-using-array-linked-list-and-arraylist-2a434555118e
Stack: Data Structure — Implementation in Java using Array, Linked List and ArrayList | by Harshit Raj | Medium
March 3, 2023 - We use the add() method to add elements to the end of the ArrayList and the remove() method to remove elements from the end of the ArrayList. import java.util.*; public class Stacks { static class Stack { static ArrayList<Integer> list = new ArrayList<>(); public static boolean isEmpty() { return (list.size() == 0); } public static void push(int data) { list.add(data); } public static int pop() { if (isEmpty() == true) return -1; int top = list.get(list.size() - 1); list.remove(list.size() - 1); return top; } public static int peek() { if (isEmpty()) return -1; return list.get(list.size() - 1); } } public static void main(String args[]) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); while (!s.isEmpty()) { System.out.println(s.peek()); s.pop(); } } }
🌐
Stack Overflow
stackoverflow.com › questions › 22546834 › how-to-push-stack-content-to-an-array-in-java › 22547283
How to push Stack content to an array in Java? - Stack Overflow
public void test(StackADT<Integer> stack) { SimpleListADT<Integer> values = new ArraySimpleList<Integer>(); // I want to push the content of the stack in the parameter to the list // using a loop; the stacks will contain integers but they will be represented // as strings, not sure if I have the for loop set up correctly for (int i = 0; i < stack.size();i++) { String s = stack.get(i); values.addToRear(s); // not sure what to put in the body of the loop here, I know the above is // incorrect because there's no get(int) method for stacks, I just need a // push in the right direction } }
🌐
CodeSpeedy
codespeedy.com › home › how to convert a stack to an array in java?
How to convert a stack to an array in Java? - CodeSpeedy
July 11, 2024 - This is done to use the Stack class without needing to specify its full package name every time. Inside the main method, a variable named stack is created of type Stack<Integer> that can hold <Integer> objects. Integers 1,2,3 and 4 are ...
🌐
Delft Stack
delftstack.com › home › howto › java › how to convert integer list to int array in java
How to Convert Integer List to Int Array in Java | Delft Stack
February 2, 2024 - It can help us get our desired results because it includes a method mapToInt() which returns an IntStream (a sequence of primitive int values). To make it more clear, let’s see it in the example below. import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numList = new ArrayList<Integer>(); numList.add(11); numList.add(22); numList.add(33); numList.add(44); numList.add(55); int[] numArray = numList.stream().mapToInt(i -> i).toArray(); for (int intValue : numArray) { System.out.println(intValue); } } }
Top answer
1 of 2
1

Try something more like this...

Before you are only reading in one number one time, which seems like you want to be used for the size of your array. After this, you continually push the same number onto the stack because you never prompt the user to enter in any other number.

The code below goes one step further than your code in that once you have learned the number from the User and set your array size, you ask for a new number over and over again to populate your array until your array is filled. This can be accomplished by prompting the user to enter a value over and over again in the for loop.

I am not really sure why exactly you need an array to do what you want to do though, consider removing the array and just using the first number entered as your bounds for the for loop rather than the array size. The array data structure is really just a waste of space as the only time you ever use it is to use its length as a stopping point in the for loop

public class NumberReverse {
      public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);


      System.out.print("Enter stack size: ");

      int number = sc.nextInt();
      int[] array = new int[number];

      Stack<Integer> stack = new Stack<Integer>();
      for(int i = 0; i < array.length; i++){
          System.out.println("Enter your number: ");
          int value = sc.nextInt();              
          stack.push(value);
      }

      while (!(stack.isEmpty())) {
          System.out.println(stack.pop());


      }    
    }
}

Based on the comment below consider trying something like the code below. You are only printing out one number for the same reasons that I have listed above...

import java.util.*; 
public class NumberReverse { 
    public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter how many numbers you want to place on the Stack: ");
        int stackSize = sc.nextInt();
        Stack<Integer> stack = new Stack<Integer>();
        for(int i = 0; i < stackSize; i++){ 
            System.out.println("Enter numbers: "); 
            int number = sc.nextInt();  
            stack.push(number); 
        }
        while (!(stack.isEmpty())) { 
            System.out.println(stack.pop()); 
        } 
    } 
} 
2 of 2
0

You have to iterate over all the numbers entered by the user, and push them on the stack:

public static void main(String[] args) {
    final Scanner sc = new Scanner(System.in);

    System.out.print("Enter numbers (finish with something else): ");

    final Stack<Integer> stack = new Stack<Integer>();

    while (sc.hasNextInt())
        stack.push(sc.nextInt());

    while (!(stack.isEmpty()))
        System.out.println(stack.pop());
}
🌐
javathinking
javathinking.com › blog › convert-element-from-stack-to-int-java
Converting Elements from a Stack to `int` in Java — javathinking.com
Sorting and Searching: Converting stack elements to int can be useful when sorting or searching for a specific integer value in the stack. Algorithm Implementations: Many algorithms, such as postfix expression evaluation, require converting stack elements to int for proper execution. import java.util.Stack; public class StackToIntExample1 { public static void main(String[] args) { // Create a stack of Integer objects Stack<Integer> stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(30); // Convert elements from stack to int and print them while (!stack.isEmpty()) { // Pop an element from the stack Integer element = stack.pop(); // Convert Integer to int int intValue = element.intValue(); System.out.println(intValue); } } }
🌐
Medium
medium.com › @christopherii_ › java-using-array-to-create-a-stack-class-b8e51f140f5f
Java: Using Array to Create a Stack Class | by Christopher | Software Developer | Medium
March 1, 2022 - If you read my previous post (re: Java Stacks: Reversing a String), you learned that Stacks are fun and easy to use. If you are not familiar working with Arrays, I suggest you study it first before you dive in this post. ... I created the Stack class that has two properties (items, and count). I used an array of integers for simplicity.
🌐
Studytonight
studytonight.com › java-examples › convert-integer-list-to-int-array-in-java
Convert Integer List to Int Array in Java - Studytonight
import java.util.*; public class StudyTonight { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); int[] arr = list.stream().mapToInt(i->i).toArray(); for (int val : arr) { System.out.println(val); } } }