Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Vector setSize() method in Java with Example
Next article icon

Vector setElementAt() method in Java with Example

Last Updated : 24 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The setElementAt() method of Java Vector 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 vector.
  • index: This is of integer type and refers to the position of the element that is to be replaced from the vector.

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.Vector.setElementAt() method:

Example 1:




// Java code to illustrate setElementAt()
  
import java.io.*;
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vector
            = new Vector<String>();
  
        // Use add() method to add elements in the vector
        vector.add("Geeks");
        vector.add("for");
        vector.add("Geeks");
        vector.add("10");
        vector.add("20");
  
        // Displaying the linkedvector
        System.out.println("Vector:"
                           + vector);
  
        // Using setElementAt() method to replace Geeks with GFG
        vector.setElementAt("GFG", 2);
        System.out.println("Geeks replaced with GFG");
  
        // Displaying the modified linkedvector
        System.out.println("The new Vector is:"
                           + vector);
    }
}
 
 
Output:
  Vector:[Geeks, for, Geeks, 10, 20]  Geeks replaced with GFG  The new Vector is:[Geeks, for, GFG, 10, 20]  

Example 2: To demonstrate ArrayIndexOutOfBoundsException




// Java code to illustrate setElementAt()
  
import java.io.*;
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vector
            = new Vector<String>();
  
        // Use add() method to add elements in the vector
        vector.add("Geeks");
        vector.add("for");
        vector.add("Geeks");
        vector.add("10");
        vector.add("20");
  
        // Displaying the linkedvector
        System.out.println("Vector:"
                           + vector);
  
        // 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 {
            vector.setElementAt("GFG", 10);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
 
 
Output:
  Vector:[Geeks, for, Geeks, 10, 20]  Trying to replace 10th element with GFG  java.lang.ArrayIndexOutOfBoundsException: 10 >= 5  


Next Article
Vector setSize() method in Java with Example
author
ankit15697
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Java - util package
  • Java-Collections
  • Java-Functions
  • Java-Vector
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Vector insertElementAt() Method in Java with Examples
    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 upwa
    3 min read
  • Vector isEmpty() Method in Java
    The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris
    2 min read
  • Vector iterator() method in Java with Examples
    iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector. Syntax: Iterator iterate_value = Vector.iterator(); Parameters: The func
    2 min read
  • Vector lastElement() Method in Java
    The java.util.vector.lastElement() method in Java is used to retrieve or fetch the last element of the Vector. It returns the element present at the last index of the vector. Syntax: Vector.lastElement() Parameters: The method does not take any parameter. Return Value: The method returns the last el
    2 min read
  • Vector lastIndexOf() Method in Java
    The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is us
    2 min read
  • Vector listIterator() method in Java with Examples
    java.util.Vector.listIterator() This method returns a list iterator over the elements of a Vector object in proper sequence. It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively. The iterator thus returned is fail-fast. This means that str
    3 min read
  • Vector removeAll() Method in Java
    The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed f
    2 min read
  • Vector removeAllElements() method in Java with Example
    The Java.util.Vector.removeAllElements() method is used to removes all components from this Vector and sets its size to zero. Syntax: Vector.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Jav
    2 min read
  • Vector removeElement() method in Java with Example
    The java.util.Vector.removeElement() method is used to remove first occurrence of particular object. If object is not found then it returns false else it returns true. If a particular object is present inside vector and removeElement() method call on that vector element then this method reduces vect
    3 min read
  • Vector removeElementAt() Method in Java
    The java.util.vector.removeElementAt(int index) method is used to remove an element from a Vector from a specific position or index. In this process the size of the vector is automatically reduced by one and all other elements after the removed element are shifted downwards by one position. Syntax:
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences