Java Program to Convert a Stack Collection Into an Object Array

You are currently viewing Java Program to Convert a Stack Collection Into an Object Array

Java program to convert a Stack collection into an Object array

// Java program to convert a Stack collection 
// into Object array

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Stack stack = new Stack();

    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.push(40);

    Object[] arr = stack.toArray();

    System.out.println("Stack: \n" + stack);
    System.out.printf("Array elements:\n%d, %d, %d, %d\n", arr[0], arr[1], arr[2], arr[3]);
  }
}

Output

Stack: 
[10, 20, 30, 40]
Array elements:
10, 20, 30, 40

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply