Home » Java programming language

Java Vector addElement() Method with Example

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

Vector Class addElement() method

  • addElement() method is available in java.util package.
  • addElement() method is used to add the given element in this Vector at the last.
  • addElement() 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.
  • addElement() method does not throw an exception at the time of adding an element.

Syntax:

    public void addElement(Element ele);

Parameter(s):

  • Element ele – represents the element to be added.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void addElement(Element ele) method 
// of Vector 

import java.util.Vector;

public class AddElementOfVector {
    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 addElement() method is to
        // add an element at the last
        v.addElement("SFDC");


        // Display Updated Vector
        System.out.println("v.addElement(SFDC): " + v);
    }
}

Output

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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.