Java Vector lastElement() Method

24 Mar 2025 | 2 min read

The lastElement() method of Java Vector class is used to get the last component of the vector.

Syntax

Following is the declaration of lastElement() method:

Parameter

This method does not accept any parameter.

Return

The lastElement() method returns the last element of the vector i.e. the component at index size()-1.

Exceptions

NoSuchElementException- This method has thrown an exception if the vector is empty.

Compatibility Version

Java 1.2 and above

Example 1

Output:

 The last element of a vector is: Python 

Example 2

Output:

 Element of a vector is: [11, 22, 33, 44] The last element of a vector is: 44 

Example 3

Output:

 The last element of a vector is: 404 Exception in thread "main" java.util.NoSuchElementException	at java.base/java.util.Vector.lastElement(Vector.java:525)	at myPackage.VectorLastElementExample3.main(VectorLastElementExample3.java:17) 
 
Next TopicJava Vector