Stack copyInto() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } Output: 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 // 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); } } Output: 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 Comment More infoAdvertise with us Next Article Stack copyInto() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Practice Tags : JavaJava-Collections Similar Reads Stack size() method in Java with Example The Java.util.Stack.size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. Syntax: Stack.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack. Below prog 2 min read Stack capacity() method in Java with Example The capacity() method of Stack Class is used to get the capacity of this Stack. That is the number of elements present in this stack container. Syntax: public int capacity() Parameters: This function accepts a parameter E obj which is the object to be added at the end of the Stack. Return Value: The 2 min read Stack setElementAt() method in Java with Example The setElementAt() method of Java Stack is used to set the component at the specified index of this vector to be the specified object. The previous component at that position is discarded. The index must be a value greater than or equal to 0 and less than the current size of the vector. Syntax: publ 2 min read Stack retainAll() method in Java with Example The retainAll() method of java.util.Stack class is used to retain from this stack all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this 3 min read Stack hashCode() method in Java with Example The Java.util.Stack.hashCode() method in Java is used to get the hashcode value of this Stack. Syntax: Stack.hashCode() Parameters: The method does not take any parameter. Return Value: The method returns hash code value of this Stack which is of Integer type. Below programs illustrate the Java.util 2 min read Stack removeAll() method in Java with Example The Java.util.Stack.removeAll(Collection col) method is used to remove all the elements from the Stack, present in the collection specified. Syntax: Stack.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from 2 min read Stack lastIndexOf() method in Java with Example The Java.util.Stack.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present in the Stack then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used 2 min read Stack firstElement() method in Java with Example The Java.util.Stack.firstElement() method in Java is used to retrieve or fetch the first element of the Stack. It returns the element present at the 0th index of the Stack Syntax: Stack.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first elem 2 min read Stack lastElement() method in Java with Example The Java.util.Stack.lastElement() method in Java is used to retrieve or fetch the last element of the Stack. It returns the element present at the last index of the Stack. Syntax: Stack.lastElement() Parameters: The method does not take any parameter. Return Value: The method returns the last elemen 2 min read Stack ensureCapacity() method in Java with Example The ensureCapacity() method of Java.util.Stack class increases the capacity of this Stack instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes t 2 min read Like