Friday, January 16, 2026
HomeLanguagesJavaStack insertElementAt() method in Java with Example

Stack insertElementAt() method in Java with Example

The Java.util.Stack.insertElementAt(element, index) method is used to insert a particular element at the specified index of the Stack. Both the element and the position is passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upward by one and hence the capacity is increased, creating a space for the new element.
Syntax: 
 

Stack.insertElementAt()

Parameters: The method accepts two parameters: 
 

  • element: This is element required to be inserted into the Stack.
  • index: This is of integer type and refers to the position where the new element is to be inserted.

Return Value: The method does not return anything.
Exception: The method throws ArrayIndexOutOfBoundsException if the index is an invalid number.
Below programs illustrate the Java.util.Stack.insertElementAt() method:
Program 1: Adding String elements into the Stack.
 

Java




// Java code to illustrate insertElementAt()
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);
 
        // Inserting element at 3rd position
        stack.insertElementAt("Hello", 2);
 
        // Inserting element at last position
        stack.insertElementAt("World", 6);
 
        // Displaying the final Stack
        System.out.println("The final Stack is "
                           + stack);
    }
}


Output: 

Stack: [Welcome, To, Geeks, 4, Geeks]
The final Stack is [Welcome, To, Hello, Geeks, 4, Geeks, World]

 

Program 2: Adding Integer elements into the Stack. 
 

Java




// Java code to illustrate insertElementAt()
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);
 
        // Inserting element at 1st position
        stack.insertElementAt(100, 0);
 
        // Inserting element at fifth position
        stack.insertElementAt(200, 4);
 
        // Displaying the final Stack
        System.out.println("The final Stack is "
                           + stack);
    }
}


Output: 

Stack: [10, 20, 30, 40, 50]
The final Stack is [100, 10, 20, 30, 200, 40, 50]

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS