Stack setElementAt() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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 Comment More infoAdvertise with us Next Article Stack setElementAt() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Practice Tags : JavaJava-Collections Similar Reads Stack search() Method in Java The java.util.Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1. If more than o 2 min read Stack removeElementAt() method in Java with Example The Java.util.Stack.removeElementAt(int index) method is used to remove an element from a Stack from a specific position or index. In this process the size of the Stack is automatically reduced by one and all other elements after the removed element are shifted downwards by one position. Syntax: Sta 2 min read Stack remove(int) method in Java with Example The Java.util.Stack.remove(int index) method is used to remove an element from a Stack from a specific position or index. Syntax: Stack.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from t 2 min read Stack removeAllElements() method in Java with Example The Java.util.Stack.removeAllElements() method is used to removes all components from this Stack and sets its size to zero. Syntax: Stack.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.u 2 min read Stack remove(Object) method in Java with Example The Java.util.Stack.remove(Object o) method is used to remove any particular element from the Stack. Syntax: Stack.remove(Object o) Parameters: This method accepts a mandatory parameter o is of the object type of Stack and specifies the element to be removed from the Stack. Return Value: Returns Tru 2 min read Stack addAll(int, Collection) method in Java with Example The addAll(int, Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a Stack. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in 2 min read Stack listIterator() method in Java with Example The listIterator() method of Java.util.Stack class is used to return a list iterator over the elements in this stack (in proper sequence). The returned list iterator is fail-fast. Syntax: public ListIterator listIterator() Return Value: This method returns a list iterator over the elements in this s 2 min read Stack listIterator(int) method in Java with Example The listIterator(int) method of Stack Class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to pre 2 min read Stack trimToSize() method in Java with Example The trimToSize() method of Stack in Java trims the capacity of an Stack instance to be the list's current capacity. This method is used to trim an Stack instance to the number of elements it contains. Syntax: public void trimToSize() Parameter: It does not accepts any parameter. Return Value: It doe 2 min read Stack lastIndexOf(Object, int) method in Java with Example The Java.util.Stack.lastIndexOf(Object element, int last_index) method is used to the last index of the first occurrence of the specified element in this Stack, searching forwards from last index, or returns -1 if the element is not found. More formally, returns the lowest last index i such that (i 3 min read Like