What does the method addElement(E obj) do in java?


The 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 add() method returns true/false but the addElement() method does not return any value.

Example

import java.util.Vector;
public class VectorDemo {
   public static void main(String[] args) {

      Vector<Integer> vec = new Vector<Integer>(4);
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      System.out.println("Initial elements in the vector :- ");
      for (Integer number : vec) {
         System.out.println("Number = " + number);
      }
      vec.addElement(12);
      System.out.println("Elements after addition :- ");
      for (Integer number : vec) {
         System.out.println("Number = " + number);
      }
   }
}

Output

Initial elements in the vector :-
Number = 4
Number = 3
Number = 2
Number = 1
Elements after addition :-
Number = 4
Number = 3
Number = 2
Number = 1
Number = 12

Rishi Raj
Rishi Raj

I am a coder

Updated on: 26-Feb-2020

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements