Java Vector lastElement() Example

The lastElement() method of java.util.Vector class will get you the last element of the vector

Signature

public E lastElement() 

Retrieves the last element of the vector and will throw java.util.NoSuchElementException if the vector is empty.

Example

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

import java.util.Vector;

public class LastElementMethodVectorExample 
{
    public static void main(String args[])
    {
     // create an empty Vector
        Vector vector1 = new Vector();
        
        // use add() method to add elements to the Vector
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element3");
        
        //Getting the last element of the vector
        System.out.println("Last element of the Vector is : "+vector1.lastElement());
    }
}

Output

Last element of the Vector is : Element3

Leave a Reply

Your email address will not be published. Required fields are marked *