java.util.Vector.firstElement() Method

Advertisements


Description

The firstElement() method is used to return the first component (the item at index 0) of this vector.

Declaration

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

public E firstElement()

Parameters

  • NA

Return Value

The method call returns the first component of this vector.

Exception

  • NoSuchElementException--This exception is returned if this vector has no components.

Example

The following example shows the usage of java.util.Vector.firstElement() method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      // create an empty Vector vec with an initial capacity of 4      
      Vector<Integer> vec = new Vector<Integer>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // let us check the first element of the vector
      System.out.println("First element is :"+vec.firstElement());                  
   }
}

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

First element is :4



Advertisements
Advertisements