Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Stack setSize() method in Java with Example
Next article icon

Stack toArray() method in Java with Example

Last Updated : 24 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The toArray() method of Stack class in Java is used to form an array of the same elements as that of the Stack. Basically, it copies all the element from a Stack to a new array.

Syntax:

Object[] arr = Stack.toArray()

Parameters: The method does not take any parameters.

Return Value: The method returns an array containing the elements similar to the Stack.

Below programs illustrate the Stack.toArray() method:

Program 1:




// 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]);
    }
}
 
 
Output:
  The Stack: [Welcome, To, Geeks, For, Geeks]  The array is:  Welcome  To  Geeks  For  Geeks  

Program 2:




// Java code to illustrate toArray()
  
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<Integer> stack = new Stack<Integer>();
  
        // Use add() method to add elements into the Stack
        stack.add(10);
        stack.add(15);
        stack.add(30);
        stack.add(20);
        stack.add(5);
        stack.add(25);
  
        // 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]);
    }
}
 
 
Output:
  The Stack: [10, 15, 30, 20, 5, 25]  The array is:  10  15  30  20  5  25  


Next Article
Stack setSize() method in Java with Example

C

code_r
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Collections
  • Java-Functions
  • Java-Stack
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Stack elementAt() method in Java with Example
    The Java.util.Stack.elementAt(int pos) method is used to fetch or retrieve an element at a specific index from a Stack. Syntax: Stack.elementAt(int pos) Parameters: This method accepts a mandatory parameter pos of integer data type that specifies the position or index of the element to be fetched fr
    2 min read
  • Stack isEmpty() method in Java with Example
    The Java.util.Stack.isEmpty() method in Java is used to check and verify if a Stack is empty or not. It returns True if the Stack is empty else it returns False. Syntax: Stack.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Stackis empty
    2 min read
  • Stack insertElementAt() method in Java with Example
    The Java.util.Stack.insertElementAt(element, index) method is used to insert a particular element at the specified index of the Stack. Both the element and the position is passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upward by one and hen
    2 min read
  • Stack clear() method in Java with Example
    The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Par
    2 min read
  • Stack indexOf(Object, int) method in Java with Example
    The Java.util.Stack.indexOf(Object element, int index) method is used to the index of the first occurrence of the specified element in this Stack, searching forwards from index, or returns -1 if the element is not found. More formally, returns the lowest index i such that (i >= index && O
    2 min read
  • Stack toArray(T[]) method in Java with Example
    The toArray(T[]) method method of Stack class in Java is used to form an array of the same elements as that of the Stack. It returns an array containing all of the elements in this Stack in the correct order; the run-time type of the returned array is that of the specified array. If the Stack fits i
    4 min read
  • Stack indexOf() method in Java with Example
    The Java.util.Stack.indexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the Stack does not contain the element. Syntax: St
    2 min read
  • Stack containsAll() method in Java with Example
    The containsAll() method of Java Stack is used to check whether two stacks contain the same elements or not. It takes one stack as a parameter and returns True if all of the elements of this stack is present in the other stack. Syntax: public boolean containsAll(Collection C) Parameters: The paramet
    2 min read
  • Stack removeRange() method in Java with Example
    The removeRange() method of Stack in Java is used to remove all elements within the specified range from an Stack object. It shifts any succeeding elements to the left. This call shortens the stack by (toIndex-fromIndex) elements where toIndex is the ending index and fromIndex is the starting index
    2 min read
  • Stack get() method in Java with Example
    The Java.util.Stack.get() method is used to fetch or retrieve an element at a specific index from a Stack. Syntax: Stack.get(int index) Parameters: This method accepts a mandatory parameter index which is of integer data type. It specifies the position or index of the element to be fetched from the
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences