The java.util.Stack.copyInto() method is used to copy all of the components from this Stack to another Stack, having enough space to hold all of the components of the Stack. It is to be noted that the index of the elements remains unchanged. The elements present in the Stack are replaced by the elements of the Stack.
Syntax:
Stack.copyInto(Object Stack[])
Parameters: The parameter Stack[] is of the type of Stack. This is the Stack into which the elements of the Stack are to be copied.
Return Value: The method is of void type and does not return any values.
Exception: The method throws NullPointerException if the Stack is NULL.
Below programs illustrates the Java.util.Stack.copyInto() method:
Program 1:
// Java code to illustrate copyInto() 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("4"); stack.add("Geeks"); // Displaying the Stack System.out.println("Stack: " + stack); // Creating an Stack String arr[] = new String[6]; arr[0] = "Hello"; arr[1] = "World"; // Displaying the initial Stack System.out.println("The initial Stack is: "); for (String str : arr) System.out.println(str); // Copying stack.copyInto(arr); // The final Stack System.out.println("The final Stack is: "); for (String str : arr) System.out.println(str); } } |
Stack: [Welcome, To, Geeks, 4, Geeks] The initial Stack is: Hello World null null null null The final Stack is: Welcome To Geeks 4 Geeks null
Program 2:
// Java code to illustrate copyInto() 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(20); stack.add(30); stack.add(40); stack.add(50); // Displaying the Stack System.out.println("Stack: " + stack); // Creating an Stack Integer arr[] = new Integer[6]; arr[0] = 50; arr[1] = 60; arr[2] = 70; arr[3] = 80; arr[4] = 90; // Displaying the initial Stack System.out.println("The initial Stack is: "); for (Integer str : arr) System.out.println(str); // Copying stack.copyInto(arr); // The final Stack System.out.println("The final Stack is: "); for (Integer str : arr) System.out.println(str); } } |
Stack: [10, 20, 30, 40, 50] The initial Stack is: 50 60 70 80 90 null The final Stack is: 10 20 30 40 50 null
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Vector copyInto() Method in Java
- Stack set() method in Java with Example
- Stack pop() Method in Java
- Stack contains() method in Java with Example
- Stack get() method in Java with Example
- Stack removeRange() method in Java with Example
- Stack subList() method in Java with Example
- Stack elementAt() method in Java with Example
- Stack toArray() method in Java with Example
- Stack toArray(T[]) method in Java with Example
- Stack retainAll() method in Java with Example
- Stack removeElementAt() method in Java with Example
- Stack isEmpty() method in Java with Example
- Stack removeAll() method in Java with Example
- Stack lastIndexOf() method in Java with Example
- Stack lastElement() method in Java with Example
- Stack setElementAt() method in Java with Example
- Stack setSize() method in Java with Example
- Stack firstElement() method in Java with Example
- Stack hashCode() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

