Stack toArray(T[]) method in Java with Example - GeeksforGeeks

24 Dec, 2018
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 in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this Stack.If the Stack fits in the specified array with room to spare (i.e., the array has more elements than the Stack), the element in the array immediately following the end of the Stack is set to null. (This is useful in determining the length of the Stack only if the caller knows that the Stack does not contain any null elements.)
Syntax:
Object[] arr1 = Stack.toArray(arr[])
Parameters: The method accepts one parameter arr[] which is the array into which the elements of the Stack are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Return Value: The method returns an array containing the elements similar to the Stack.
Exception: The method might throw two types of exception:
ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the Stack.
NullPointerException: If the array is Null, then this exception is thrown.
Below program illustrates the working of the Stack.toArray(arr[]) method.
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]);    }}
The Stack: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks

Program 2: When array is less than 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[1];        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]);    }}
The Stack: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks

Program 3: When array is more than 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[10];        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]);    }}
The Stack: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
null
null
null
null
null

Program 4: To demonstrate NullPointerException
// 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);          try {            // Creating the array            String[] arr = null;            // using toArray()            // Since arr is null            // Hence exception will be thrown            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]);        }        catch (Exception e) {            System.out.println("Exception: " + e);        }    }}
The Stack: [Welcome, To, Geeks, For, Geeks]
Exception: java.lang.NullPointerException

Java - util package
Java-Collections
Java-Functions
Java-Stack
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples
HashMap in Java with Examples
How to iterate any Map in Java
Initialize an ArrayList in Java
Interfaces in Java