Stack add(int, Object) method in Java with Example

The add(int, Object) method of Stack Class inserts an element at a specified index in the Stack. It shifts the element currently at that position (if any) and any subsequent elements to the right (will change their indices by adding one).

Syntax:

void add(int index, Object element)

Parameters: This method accepts two parameters as described below.

  • index: The index at which the specified element is to be inserted.
  • element: The element which is needed to be inserted.

Return Value: This method does not return any value.

Exception: The method throws IndexOutOfBoundsException if the specified index is out of range of the size of the Stack.

Below program illustrates the working of java.util.Stack.add(int index, Object element) method:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate boolean add(Object element)
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");
  
        // Output the present Stack
        System.out.println("The Stack is: " + stack);
  
        // Adding new elements
        stack.add(2, "Last");
        stack.add(4, "Element");
  
        // Printing the new Stack
        System.out.println("The new Stack is: " + stack);
    }
}

chevron_right


Output:

The Stack is: [Geeks, for, Geeks, 10, 20]
The new Stack is: [Geeks, for, Last, Geeks, Element, 10, 20]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate
// boolean add(Object element)
  
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 in the Stack
        stack.add(10);
        stack.add(20);
        stack.add(30);
        stack.add(40);
        stack.add(50);
  
        // Output the present Stack
        System.out.println("The Stack is: "
                           + stack);
  
        // Adding new elements
        stack.add(0, 100);
        stack.add(3, 200);
  
        // Printing the new Stack
        System.out.println("The new Stack is: "
                           + stack);
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.