Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Stack.insertElementAt() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Stack class is a Last-In-First-Out (LIFO)The element inserted first is processed last and the element inserted last is processed first. stack of objects.

The insertElementAt() method of the Stack class can be used to add an element at the specific index of the Stack object. The index of the elements present at and after the passed index will be shifted upward (increased by 1).

Syntax

public void insertElementAt(E obj,int index)

Parameters

This method takes two parameters:

  1. The element to be added to the Stack object.

  2. The index at which the new element is to be added. The index should be positive and less than or equal to the size of the Stack, as shown below. Otherwise, ArrayIndexOutOfBoundsException will be thrown.

index >= 0 && index <= size()

Return value

insertElementAt() doesn’t return a value.

Code

The code below demonstrates the use of the Stack.insertElementAt() method:

import java.util.Stack;
class InsertElementAt {
public static void main( String args[] ) {
// Creating Stack
Stack<Integer> stack = new Stack<>();
// add elememts
stack.push(10);
stack.push(30);
stack.push(50);
System.out.println("The stack is: " + stack); // [10,30,50]
stack.insertElementAt(20, 1); // [10,20,30,50]
System.out.println("\nAfter calling insertElementAt(20,1). The stack is: " + stack);
stack.insertElementAt(40, 3); // [10,20,30,40,50]
System.out.println("\nAfter calling insertElementAt(40,3). The stack is: " + stack);
}
}

Please click the Run button above to see how the insertElementAt() method works.

Explanation

  • In line 5, we create a Stack object.

  • In lines 8 to 10, we add three elements (10,30,50) to the created Stack object.

  • In line 14, we use the insertElementAt() method of the Stack object to add element 20 at index 1. Now, stack will contain 10,20,30,50.

  • In line 17, we use the insertElementAt() method of the Stack object to add element 40 at index 3. Now, stack will contain 10,20,30,40,50.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring