What is the Vector.setElementAt method in Java?

What is the Vector.setElementAt method in Java? Answer

The Vector class is a growable array of objects. The elements of Vector can be accessed using an integer index and the size of a Vector can be increased or decreased.

Read more about Vector here.

The setElementAt method of the Vector class can be used to replace the element present at the specific index of the vector object.

Syntax

public void setElementAt(E obj,int index)

Argument

This method takes two arguments.

  1. The element to be replaced in the vector object.
  2. The index at which the new element is to be replaced. The index should be positive and less than the size of the vector object. Otherwise, ArrayIndexOutOfBoundsException will be thrown.

index >= 0 && index < size()

Return value

This method doesn’t return any value.

Code

import java.util.Vector;
class SetElementAt {
    public static void main( String args[] ) {
        // Creating Vector
        Vector<Integer> vector = new Vector<>();

        // add elememts 
        vector.add(10);
        vector.add(40);
        vector.add(30);

        System.out.println("The Vector is: " + vector); // [10,40,30]

        vector.setElementAt(20, 1); // [10,20,30]
        System.out.println("\nAfter calling setElementAt(20,1). The Vector is: " + vector);
        

    }
}

Explanation

In the code above:

  • In line 5: We created a vector object.
  • In lines 8 to 10: We added three elements, 10,40,30, to the created vector object.
  • In line 14: We used the setElementAt method of the vector object to replace the element present at index 1 with the value 20. Now, the vector will be 10,20,30.
What is the Vector.setElementAt method in Java? Review:

In our experience, we suggest you solve this What is the Vector.setElementAt method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the Vector.setElementAt method in Java?

Find on Educative

Conclusion:

I hope this What is the Vector.setElementAt method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.