java.util.Vector.lastElement() Method
Advertisements
Description
The lastElement() method is used to return the last component of the vector.
Declaration
Following is the declaration for java.util.Vector.lastElement() method
public E lastElement()
Parameters
NA
Return Value
The method call returns the last component of the vector, i.e., the component at index size() - 1.
Exception
NoSuchElementException--This exception is thrown if this vector is empty
Example
The following example shows the usage of java.util.Vector.lastElement() method.
package com.tutorialspoint;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec with an initial capacity of 4
Vector vec = new Vector(4);
// use add() method to add elements in the vector
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
// let us print the last element of the vector
System.out.println("Last elemet: "+vec.lastElement());
}
}
Let us compile and run the above program, this will produce the following result.
Last elemet: 1