In the previous article, we have discussed about Java Vector clear( ) Method with Example
In this article, we are going to see the use of Java Vector lastIndexOf() method along with examples.
Java Vector lastIndexOf( ) Method with Example
Let’s see different ways to get the last index of an element present in a vector. The last index of an element means if the same element is present multiple times in the vector then this method will return the last index where the element is present.
lastIndexOf(Object o)
This java.util.Vector.lastIndexOf(Object o)
method returns the index of the last occurrence of an element. We can just pass the element into the lastIndexOf( )
method. It returns the index of the last occurrence of the element else if the element does not exist it returns -1.
Syntax:
vectorName.lastIndexOf(Object o )
Where,
vectorName
refers to the name of the Vector.Object o
refers to the element whose index you want to find out.
Example: Java Vector lastIndexOf(Object o) Method – Example with String Type Vector
Approach:
- Create a string vector.
- Add elements to the vector.
- Print the elements of the vector.
- Print the index of the last occurrence of an element by passing the element into the
lastIndexOf()
method. - If the element does not exist in the vector the method returns -1.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a Vector of string datatype Vector<String> vec = new Vector<String>(); // Adding some elements to the vector vec.add("One"); vec.add("Two"); vec.add("Three"); vec.add("Four"); vec.add("Five"); vec.add("Three"); vec.add("Four"); vec.add("Five"); vec.add("One"); vec.add("Two"); vec.add("Three"); vec.add("Four"); vec.add("Five"); // Prints the vector elements System.out.println("The vector elements are "+vec); // Prints the index of last occurrence of the element 'Five' System.out.println("The last occurrence of element 'Five' is at index "+vec.lastIndexOf("Five")); // Trying to find the index of a non exitent element // The function should return -1 System.out.println("The first occurrence of element 'Twelve' is at "+vec.indexOf("Twelve")); } }
Output: The vector elements are [One, Two, Three, Four, Five, Three, Four, Five, One, Two, Three, Four, Five] The last occurrence of element 'Five' is at index 12 The first occurrence of element 'Twelve' is at -1
lastIndexOf(Object o, int index_to_search_from)
When there are multiple occurrences of the same element in a vector. To display the index of an element from a certain index from behind we can just pass the element into the lastIndexOf( )
method with the index_to_search from
. It returns the index of the first occurrence of the element else if the element does not exist it returns -1. Means here the searching of last index of element takes place backward manner from the specified index.
Syntax:
vectorName.lastIndexOf(Object o, , int index_to_search_from)
Where,
vectorName
refers to the name of the Vector.Object o
refers to the element whose index you want to find out.int index_to_search_from
refers to the index from which you start the search(backward)
Example: Java Vector lastIndexOf(Object o, int index_to_search_from) Method – Example with String Type Vector
Approach:
- Create a string vector.
- Add elements to the vector.
- Print the elements of the vector.
- Print the index of the first occurrence of an element by passing the element and index to search from into the
lastIndexOf()
method. - If the element does not exist in the vector the method returns -1.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a Vector of string datatype Vector<String> vec = new Vector<String>(); // Adding some elements to the vector vec.add("One"); vec.add("Two"); vec.add("Three"); vec.add("Four"); vec.add("Five"); vec.add("Three"); vec.add("Four"); vec.add("Five"); vec.add("One"); vec.add("Two"); vec.add("Three"); vec.add("Four"); vec.add("Five"); // Prints the vector elements System.out.println("The vector elements are "+vec); // Prints the index of last occurrence of the element 'One' after index 4 from behind System.out.println("The last occurrence of element 'One' from behind after index 4 is at index "+vec.lastIndexOf("One",4)); // Trying to find the index of a non exitent element // The function should return -1 System.out.println("The first occurrence of element 'Twelve' is at "+vec.indexOf("Twelve")); } }
Output: The vector elements are [One, Two, Three, Four, Five, Three, Four, Five, One, Two, Three, Four, Five] The last occurrence of element 'One' from behind after index 4 is at index 0 The first occurrence of element 'Twelve' is at -1
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: