- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace an element at a specified index of the Vector in Java
An element in a Vector can be replaced at a specified index using the java.util.Vector.set() method. This method has two parameters i.e the index at which the Vector element is to be replaced and the element that it should be replaced with. Vector.set() method returns the element that was at the position specified at the index previously.
A program that demonstrates this is given as follows:
Example
import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(7); vec.add(3); vec.add(9); vec.add(1); vec.add(5); System.out.println("The Vector elements are: " + vec); vec.set(1, 6); System.out.println("The Vector elements are: " + vec); } }
The output of the above program is as follows:
The Vector elements are: [7, 3, 9, 1, 5] The Vector elements are: [7, 6, 9, 1, 5]
Now let us understand the above program.
The Vector is created. Then Vector.add() is used to add the elements to the Vector. Then Vector.set() is used to replace the element 3 with element 6 at index 1. Then the Vector is displayed. A code snippet which demonstrates this is as follows:
Vector vec = new Vector(5); vec.add(7); vec.add(3); vec.add(9); vec.add(1); vec.add(5); System.out.println("The Vector elements are: " + vec); vec.set(1, 6); System.out.println("The Vector elements are: " + vec);
- Related Articles
- Add an element to specified index of ArrayList in Java
- Insert an element into Collection at specified index in C#
- Insert an element into the ArrayList at the specified index in C#
- Remove element at specified index of Collection in C#
- Remove the element at the specified index of the ArrayList in C#
- Golang Program to insert an element into the array at the specified index
- Python Program to insert an element into the array at the specified index
- How to find the index of an element in a vector in R?
- Insert the specified element at the specified position in Java CopyOnWriteArrayList
- Replace an element of a Java LinkedList
- Get or set the element at specified index in Collection in C#
- Replace all occurrences of specified element of ArrayList with Java Collections
- Search an element of Vector in Java
- Get or set the element at the specified index in ArrayList in C#
- Gets or sets the element at the specified index in StringCollection in C#

Advertisements