Vector insertElementAt() Method in Java with Examples Last Updated : 24 May, 2023 Comments Improve Suggest changes Like Article Like Report insertElementAt() method of Vector class present inside java.util package is used to insert a particular element at the specified index of the Vector. Both the element and the position are 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: Vector.insertElementAt() Parameters: The method accepts two parameters: element: It is required to be inserted into the vector.index: It refers to the position where the new element is to be inserted(integer type) Exceptions Thrown: ArrayIndexOutOfBoundsException if the index is an invalid number. Let us now add elements by proposing examples for both string elements and integer elements and applying our method over both of them just only to be familiar with the working of this method with different primitive datatypes. Example 1: Java // Java Program to illustrate insertElementAt() // Method of Vector class by // Adding String elements into the Vector // Importing required classes import java.util.*; // Main class public class GFG { // Main driver method public static void main(String args[]) { // Creating an empty vector of string type Vector<String> vec_tor = new Vector<String>(); // Adding custom elements into the vector // using add() method vec_tor.add("Welcome"); vec_tor.add("To"); vec_tor.add("Geeks"); vec_tor.add("4"); vec_tor.add("Geeks"); // Printing elements of vector System.out.println("Vector: " + vec_tor); // Inserting element at 3rd position // Custom specified vec_tor.insertElementAt("Hello", 2); // Inserting element at last position vec_tor.insertElementAt("World", 6); // Printing elements of final vector System.out.println("The final vector is " + vec_tor); } } Output: Vector: [Welcome, To, Geeks, 4, Geeks] The final vector is [Welcome, To, Hello, Geeks, 4, Geeks, World] Example 2: Java // Java Program to illustrate insertElementAt() // Method of Vector class by // Adding Integer Elements into the Vector // Importing required classes import java.util.*; // Main class public class GFG { // Main driver method public static void main(String args[]) { // Creating an empty Vector of integer type Vector<Integer> vec_tor = new Vector<Integer>(); // Adding elements into the vector // using add() method vec_tor.add(10); vec_tor.add(20); vec_tor.add(30); vec_tor.add(40); vec_tor.add(50); // Printing the current elements of vector System.out.println("Vector: " + vec_tor); // Inserting element at 1st position vec_tor.insertElementAt(100, 0); // Inserting element at 5th position vec_tor.insertElementAt(200, 4); // Printing the final elements of Vector System.out.println("The final vector is " + vec_tor); } } Output: Vector: [10, 20, 30, 40, 50] The final vector is [100, 10, 20, 30, 200, 40, 50] Comment More infoAdvertise with us Next Article Vector insertElementAt() Method in Java with Examples K kundankumarjha Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-Vector +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Vector clear() Method in Java The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:Vector.clear( 2 min read Java Vector clone() Method In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.Example 1: In this example, we use the cl 3 min read Java Vector contains() Method The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not.Java// Java program to demonstrate the use // of the contains() method with Strings import j 3 min read Java Vector containsAll() Method In Java, the containsAll() method is used to check if the Vector contains all the elements of a specified Collection or not.Example 1: Below program demonstrating the use of containsAll() method with Strings.Java// Java Program to demonstrate the use of // containsAll() with Strings import java.util 2 min read Java Vector copyInto() Method In Java, the copyInto() method is used to copy all the components from one vector to another array, having enough space to hold all of the components of the vector. It is to be noted that the index of the elements remains unchanged. The elements present in the array are replaced by the elements of t 3 min read Java Vector elementAt() Method In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based.Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Integer 2 min read Java Vector elements() Method In Java, the elements() method is used to return an enumeration of the elements in the Vector. This method allows you to iterate over the elements of a Vector using an Enumeration, which provides methods for checking if there are more elements and retrieving the next element.Example 1: Below is the 2 min read Vector ensureCapacity() method in Java with Example The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes 2 min read Vector equals() Method in Java The java.util.vector.equals(Object obj) method of Vector class in Java is used verify the equality of an Object with a vector and compare them. The list returns true only if both Vector contains same elements with same order. Syntax: first_vector.equals(second_vector) Parameters: This method accepts 2 min read Vector firstElement() Method in Java The java.util.vector.firstElement() method in Java is used to retrieve or fetch the first element of the Vector. It returns the element present at the 0th index of the vector Syntax: Vector.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first 2 min read Like