Java Vector addElement() Method



Description

The Java Vector addElement(E obj) method is used to add the specified component to the end of this vector and increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. This addElement() method is identical in functionality to the add(Object) method. The addElement() method returns true/false but the addElement() method does not return any value.

Declaration

Following is the declaration for java.util.Vector.addElement() method

public void addElement(E obj)

Parameters

obj − It refers to the component to be added.

Return Value

The return type is void

Exception

NA

Adding an Element to a Vector of Integer Example

The following example shows the usage of Java Vector addElement(E) method to add Integers. We're adding couple of Integers to the Vector object using addElement() method calls per element and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      Vector<Integer> vector = new Vector<>();

      // use addElement() method to add elements in the vector
      vector.addElement(20);
      vector.addElement(30);
      vector.addElement(20);
      vector.addElement(30);
      vector.addElement(15);
      vector.addElement(22);
      vector.addElement(11);

      // let us print all the elements available in vector
      for (Integer number : vector) {
         System.out.println("Number = " + number);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 20
Number = 30
Number = 20
Number = 30
Number = 15
Number = 22
Number = 11

Adding an Element to a Vector of String Example

The following example shows the usage of Java Vector addElement(E) method to add Strings. We're adding couple of strings to the Vector object using addElement() method calls per element and then printing the Vector using its toString() method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      Vector<String> vector = new Vector<>();

      // use addElement() method to add elements in the vector
      vector.addElement("Welcome");
      vector.addElement("To");
      vector.addElement("Tutorialspoint");
      System.out.println("Vector = " + vector);      
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [Welcome, To, Tutorialspoint]

Adding an Element to a Vector of Object Example

The following example shows the usage of Java Vector addElement(E) method to add Student objects. We're adding couple of Student objects to the Vector object using addElement() method calls per element and then printing the Vector using its toString() method.

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 addElement() method to add elements in the vector
      vector.addElement(new Student(1, "Julie"));
      vector.addElement(new Student(2, "Robert"));
      vector.addElement(new Student(3, "Adam"));
      System.out.println("Vector = " + vector);      
   }
}

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 −

Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_vector.htm
Advertisements