JAVA program for Vector.lastIndexOf() Method

JAVA program for Vector.lastIndexOf() Method

This JAVA program is to demonstrate the use of Vector.lastIndexOf() method.

The lastIndexOf() method returns the last index  of the element specified in the brackets, that is if we have two or more duplicate elements , then it will return the index of the last duplicate element in the Vector.

Note : If the element at the index specified in the brackets is not present, then it returns -1.

Program

import java.util.*;

class v9
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int i,n;
		Vector<Integer> v = new Vector<Integer>();				
		System.out.println("how many elements do you want to insert?");
		n=sc.nextInt();
		
		System.out.println("Start inserting the elements");
		for(i=0;i<n;i++)
		{
			v.add(sc.nextInt());		
		}
		System.out.println("Enter the element of which you want to find the index of");
		int el = sc.nextInt();
		System.out.println("The LAST Index Of the element("+el+") in the Vector = "+v.lastIndexOf(el));
	}
}

Output

Share Me!