Javatpoint Logo
Javatpoint Logo

Java Vector lastElement() Method

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

Test it Now

Output:

The last element of a vector is: Python

Example 2

Test it Now

Output:

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

Example 3

Test it Now

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA