Javatpoint Logo
Javatpoint Logo

Java Vector firstElement() Method

The firstElement() method of Java Vector class is used to get the first element ( at index 0 ) of the vector which is in use.

Syntax:

Following is the declaration of firstElement() method:

Parameter:

This method does not accept any parameter.

Returns:

The firstElement() method returns the first component of the vector which is in use.

Exceptions:

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

Compatibility Version:

Java 1.2 and above

Example 1:

Test it Now

Output:

First element of the vector is = 91

Example 2:

Test it Now

Output:

First color of this vector = White

Example 3:

Test it Now

Output:

Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Vector.firstElement(Vector.java:511)
	at myPackage.VectorFirstElementExample3.main(VectorFirstElementExample3.java:8)

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