Thursday, June 11, 2026
HomeLanguagesJavaStack setElementAt() method in Java with Example

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:

public void setElementAt(E element, int index)

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

  • element: It is the new element by which the existing element will be replaced and is of the same object type as the stack.
  • index: This is of integer type and refers to the position of the element that is to be replaced from the stack.

Return Value: This method do not return anything.

Exception: This method throws ArrayIndexOutOfBoundsException if the index is out of range (index = size())

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

Example 1:




// Java code to illustrate setElementAt()
  
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 setElementAt() method to replace Geeks with GFG
        stack.setElementAt("GFG", 2);
        System.out.println("Geeks replaced with GFG");
  
        // Displaying the modified linkedstack
        System.out.println("The new Stack is:"
                           + stack);
    }
}


Output:

Stack:[Geeks, for, Geeks, 10, 20]
Geeks replaced with GFG
The new Stack is:[Geeks, for, GFG, 10, 20]

Example 2: To demonstrate ArrayIndexOutOfBoundsException




// Java code to illustrate setElementAt()
  
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 setElementAt() 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.setElementAt("GFG", 10);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

Stack:[Geeks, for, Geeks, 10, 20]
Trying to replace 10th element with GFG
java.lang.ArrayIndexOutOfBoundsException: 10 >= 5
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS