Stack copyInto() method in Java with Example
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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:
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
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