Open In App

Stack set() method in Java with Example

The set() method of Java Stack is used to replace any particular element in the stack created using the Stack class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method.

Syntax:



public E set(int index, Object element)

Parameters: This function accepts two parameters as shown in the above syntax and described below.

Return Value: The method returns the previous value from the stack that is replaced with the new value.



Exception: This method throws following exceptions:

Below program illustrate the Java.util.Stack.set() method:

Example 1:




// Java code to illustrate set()
  
import java.io.*;
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 in the stack
        stack.add("Geeks");
        stack.add("for");
        stack.add("Geeks");
        stack.add("10");
        stack.add("20");
  
        // Displaying the linkedstack
        System.out.println("Stack:"
                           + stack);
  
        // Using set() method to replace Geeks with GFG
        System.out.println("The Object that is replaced is: "
                           + stack.set(2, "GFG"));
  
        // Using set() method to replace 20 with 50
        System.out.println("The Object that is replaced is: "
                           + stack.set(4, "50"));
  
        // Displaying the modified linkedstack
        System.out.println("The new Stack is:"
                           + stack);
    }
}

Output:
Stack:[Geeks, for, Geeks, 10, 20]
The Object that is replaced is: Geeks
The Object that is replaced is: 20
The new Stack is:[Geeks, for, GFG, 10, 50]

Example 2: To demonstrate IndexOutOfBoundException




// Java code to illustrate set()
  
import java.io.*;
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 in the stack
        stack.add("Geeks");
        stack.add("for");
        stack.add("Geeks");
        stack.add("10");
        stack.add("20");
  
        // Displaying the linkedstack
        System.out.println("Stack:"
                           + stack);
  
        // Using set() method to replace 10th with GFG
        // and the 10th element does not exist
        System.out.println("Trying to replace 10th "
                           + "element with GFG");
  
        try {
            stack.set(10, "GFG");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:
Stack:[Geeks, for, Geeks, 10, 20]
Trying to replace 10th element with GFG
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10

Article Tags :