The Java Vector set(int index,E element) method is used to replace the element at the specified position in this Vector with the specified element.
DeclarationFollowing is the declaration for java.util.Vector.set() method
public E set(int index,E element)Parameters
index − This is the index of the element to be replaced.
element − This is the element to be stored at the specified position.
The method call returns the element that was previously stored at the specified position.
ExceptionArrayIndexOutOfBoundsException − This exception is thrown if the accessed index is out of range.
Updating an Element of a Vector of Integer by Index ExampleThe following example shows the usage of Java Vector set(index, element) method. We're adding couple of Integers to the Vector object using add() method calls per element and using set(index, element) method, we're insert one of the element by index in between and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Integer> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(20); vector.add(30); vector.add(20); vector.add(30); vector.add(15); vector.add(22); vector.add(11); // insert an element at index 2 vector.set(2, 50); // let us print the 3rd element System.out.println("3rd Element = " + vector.get(2)); } }Output
Let us compile and run the above program, this will produce the following result −
3rd Element = 50Updating an Element of a Vector of String by Index Example
The following example shows the usage of Java Vector set(index, element) method. We're adding couple of Strings to the Vector object using add() method calls per element and using set(index, element) method, we're insert one of the element by index in between and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<String> vector = new Vector<>(); // use add() method to add elements in the vector vector.add("Welcome"); vector.add("To"); vector.add("Tutorialspoint"); // insert an element at index 2 vector.set(2, "World Of"); // let us print the 3rd element System.out.println("3rd Element = " + vector.get(2)); } }Output
Let us compile and run the above program, this will produce the following result −
3rd Element = World OfUpdating an Element of a Vector of Student by Index Example
The following example shows the usage of Java Vector set(index, element) method. We're adding couple of Student objects to the Vector object using add() method calls per element and using set(index, element) method, we're insert one of the element by index in between and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Student> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(new Student(1, "Julie")); vector.add(new Student(2, "Robert")); vector.add(new Student(3, "Adam")); // insert an element at index 2 vector.set(2, new Student(4, "Jene")); // let us print the 3rd element System.out.println("3rd Element = " + vector.get(2)); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }Output
Let us compile and run the above program, this will produce the following result −
3rd Element = [ 4, Jene ]
java_util_vector.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4