Home » Java programming language

Java Vector set() Method with Example

Vector Class set() method: Here, we are going to learn about the set() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 19, 2020

Vector Class set() method

  • set() method is available in java.util package.
  • set() method is used to replace the old element with the given element (ele) when it exists otherwise it sets the given element at the given indices in this Vector.
  • set() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • set() method may throw an exception at the time of replacing the element.
    ArrayIndexOutOfBoundsException: This exception may throw when the given first parameter is not in a range.

Syntax:

    public Element set(int indices, Element ele);

Parameter(s):

  • int indices – represents the indices of replacing element.
  • Element ele – represents the element to be set at the given indices.

Return value:

The return type of the method is Element, it returns the old element at the given indices when exists.

Example:

// Java program to demonstrate the example 
// of Element set(int indices, Element ele) method 
// of Vector 

import java.util.*;

public class SetOfVector {
 public static void main(String[] args) {
  // Instantiates a Vector object  with
  // initial capacity of "10"
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add the
  // elements in this v
  v.add("C");
  v.add("C++");
  v.add("JAVA");

  // Display Vector
  System.out.println("v: " + v);

  // By using set() method is to
  // replace the element JAVA at the 
  // indices 2 with the given element
  // SFDC
  v.set(2, "SFDC");

  // Display updated vector
  System.out.println("v.set(2, SFDC): " + v);
 }
}

Output

v: [C, C++, JAVA]
v.set(2, SFDC): [C, C++, SFDC]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.