Java Utility Library

Java Vector - lastElement() Method



The java.util.Vector.lastElement() method returns the last element (element at index = Vector.size()-1) of the vector.

Syntax

public E lastElement()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns the last element of the vector.

Exception

Throws NoSuchElementException, if the vector is empty.

Example:

In the example below, the java.util.Vector.lastElement() method is used to get the value of last element of the vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.addElement(50);
    MyVector.addElement(10);
    MyVector.addElement(300);

    //checking vector
    System.out.println("Last Element of Vector is: " + MyVector.lastElement());
  }
}

The output of the above code will be:

Last Element of Vector is: 300

❮ Java.util - Vector