Java Vector elements() elementAt() firstElement() lastElement() Methods Tutorial

In this section, we will learn what the Vector elements(), elementAt(), firstElement(), lastElement() methods are and how to use them in Java.

What is Java Vector elements() Method?

The Java Vector elements() method is used to return an enumeration object of a vector which allows us to enumerate the elements of that vector.

Note: The return object of this method is of type Enumeration. This object has two methods:

  • hasMoreElements(): this method tells us if there’s still another element that could be returned from this Enumeration object. (It’s a true false method. If there’s an element, the return value becomes true. Otherwise the value false returns).
  • nextElement(): this method returns the next element of the target vector object.

Java Vector elements() Method Syntax:

public Enumeration<E> elements()

elements() Method Parameters:

The method does not take an argument.

elements() Method Return Value:

As mentioned at the beginning of this section, the return value of this method is an object of type Enumeration.

elements() Method Exceptions:

The method does not throw an exception.

Example: using Vector elements() method

import java.util.Vector; 
import java.util.ArrayList;
import java.util.Enumeration; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        Enumeration<String> enm = vector.elements();
        while(enm.hasMoreElements()){
            System.out.println(enm.nextElement());
        }

    }
}

Output:

Jack

Omid

Ellen

John

What is Java Vector elementAt() Method?

The Java Vector elementAt() method is used to return the element of a vector object at the specified index.

Note: we set this index number as the argument of this method.

Java Vector elementAt() Method Syntax:

public E elementAt(int index)

elementAt() Method Parameters:

The method takes one argument and that is the index number where we want to get the element from there.

elementAt() Method Return Value:

The return value of this method is the value that was in the index we’ve specified as the argument of the method.

elementAt() Method Exceptions:

The method might throw one exception and that is:

ArrayIndexOutOfBoundsException: We get this exception if we put an index number that is out of the range of the target vector object. For example, if the vector’s indexes are ranged from 0 to 10 but we put the value 15 instead!

Example: using Vector elementAt() method

import java.util.Vector; 
import java.util.ArrayList;
import java.util.Enumeration; 
public class Main {

    public static void main(String[] args){
        
      Vector <String> vector = new Vector<>();
      vector.add("Jack");
      vector.add("Omid");
      vector.add("Ellen");
      vector.add("John");

      System.out.println(vector.elementAt(2));
    }
}

Output:

Ellen

What is Java Vector firstElement() Method?

The Java Vector firstElement() method is used to return the first element of a vector object.

Basically, using this method, we get the first element that is at the index 0 of a vector object.

Java Vector firstElement() Method Syntax:

public E firstElement()

firstElement() Method Parameters:

The method does not take an argument.

firstElement() Method Return Value:

The return value of this method is the element that was at the index 0 of the target vector object.

firstElement() Method Exceptions:

NoSuchElementException: We get this exception if the target vector object doesn’t have any element in its body!

Example: using Vector firstElement() method

import java.util.Vector; 
import java.util.ArrayList;
import java.util.Enumeration; 
public class Main {

    public static void main(String[] args){
        
      Vector <String> vector = new Vector<>();
      vector.add("Jack");
      vector.add("Omid");
      vector.add("Ellen");
      vector.add("John");

      System.out.println(vector.firstElement());
    }
}

Output:

Jack

What is Java Vector lastElement() Method?

The Java Vector lastElement() method is used to return the last element of a vector object.

This last method of a vector object is the element that is in the index number [vector.size()-1].

Java Vector lastElement() Method Syntax:

public E firstElement()

lastElement() Method Parameters:

The method does not take an argument.

lastElement() Method Return Value:

The return value of this method is the last element of the target vector object.

lastElement() Method Exceptions:

NoSuchElementException: We get this exception if the target vector object doesn’t have any element in its body!

Example: using Vector lastElement() method

import java.util.Vector; 
import java.util.ArrayList;
import java.util.Enumeration; 
public class Main {

    public static void main(String[] args){
        
      Vector <String> vector = new Vector<>();
      vector.add("Jack");
      vector.add("Omid");
      vector.add("Ellen");
      vector.add("John");

      System.out.println(vector.lastElement());
    }
}

Output:

John

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies