In this section, we will learn what the Vector set(), setElementAt(), and insertElementAt() methods are and how to use them in Java.
What is Java Vector set() Method?
The Java Vector set() method is used to replace an element that is currently in a vector object with a new value.
Java Vector set() Method Syntax:
set public E set(int index, E element)
set() Method Parameters:
The method takes two arguments:
- The first argument is the index of the element that we want to replace it with a new element.
- The second argument is the new value that wants to be replaced with the old value.
set() Method Return Value:
The return value of this method is the old element in the specified index that was replaced with a new one.
set() Method Exceptions:
The method might throw one exception:
IndexOutOfBoundsException: This exception will be thrown if the index number (the first argument) is out of the range. For example, if the target vector object indexes are from 0 to 100, but we set the value 400 instead!
Example: using Vector set() method
import java.util.Vector; public class Main { public static void main(String[] args){ Vector <Integer> vector = new Vector<>(); vector.addElement(1); vector.addElement(2); vector.addElement(3); vector.addElement(4); vector.addElement(5); vector.set(0 , 1000); for(int i: vector){ System.out.println(i); } } }
Output:
1000 2 3 4 5
What is Java Vector setElementAt() Method?
The Java Vector setElementAt() method is used to replace an element that is currently in a vector object, with a new value.
Note: this method works the same as the set() method that you’ve seen at the beginning of this section.
Java Vector setElementAt() Method Syntax:
public void setElementAt(E obj, int index)
setElementAt() Method Parameters:
The method takes two arguments:
- The first argument is the new element that we want to replace it with a value that is currently in a vector object.
- The second argument is the index number of the value in a vector object that we want to replace it with a new value (the first argument of the method).
setElementAt() Method Return Value:
The return value of this method is void.
setElementAt() Method Exceptions:
The method might throw the same exception that the set() method might throw.
Example: using Vector setElementAt() method
import java.util.Vector; public class Main { public static void main(String[] args){ Vector <Integer> vector = new Vector<>(); vector.addElement(1); vector.addElement(2); vector.addElement(3); vector.addElement(4); vector.addElement(5); vector.setElementAt(1000 , 0); for(int i: vector){ System.out.println(i); } } }
Output:
1000 2 3 4 5
What is Java Vector insertElementAt() Method?
The Java Vector insertElementAt() method is used to add a new element to a vector object.
Note: this method takes an index number and the new value that we want to be added to the target vector object. If there’s an element currently in that index and more elements afterward, they will be pushed one level forward so that new element can be added to the vector.
Basically, using this method, no element gets removed or replaced in the vector object.
Java Vector insertElementAt() Method Syntax:
public void insertElementAt(E obj, int index)
insertElementAt() Method Parameters:
The method takes two arguments:
- The first argument is the new element that wants to be added to a vector object.
- The second argument is the index number where we want this new element to be added.
insertElementAt() Method Return Value:
The return value of this method is void.
insertElementAt() Method Exceptions:
The method might throw the same exception as the set() method you’ve seen at the beginning of this section.
Example: using Vector insertElementAt() method
import java.util.Vector; public class Main { public static void main(String[] args){ Vector <Integer> vector = new Vector<>(); vector.addElement(1); vector.addElement(2); vector.addElement(3); vector.addElement(4); vector.addElement(5); vector.insertElementAt(1000 , 0); for(int i: vector){ System.out.println(i); } } }
Output:
1000 1 2 3 4 5