Java Vector indexOf( ) Method with Example

In the previous article, we have discussed about Java Vector lastIndexOf( ) Method with Example

In this article, we are going to see the use of Java Vector indexOf( ) method along with examples.

Java Vector indexOf( ) Method with Example

Let’s see different ways to find the index of an element in the vector.

indexOf(Object o)

This java.util.Vector.indexOf(Object o) method displays the index of the first occurrence of an element by passing the element into the indexOf( ) method. If the element does not exist in the vector the method returns -1.

Syntax:

vectorName.indexOf(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 indexOf(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 first occurrence of an element by passing the element into the indexOf() 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 first occurrence of the element 'Three'
       System.out.println("The first occurrence of element 'Three' is at index "+vec.indexOf("Three"));
       // 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 first occurrence of element 'Three' is at index 2
The first occurrence of element 'Twelve' is at -1

indexOf(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 after a certain index we can just pass the element into the indexOf( ) method with the index_to_search from. If the element does not exist in the vector the method returns -1.

Syntax:

vectorName.indexOf(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 it will start the search.

Example- Java Vector indexOf(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 next occurrence of an element by passing the element and the index_to_check_from into the indexOf() 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 next occurrences of the element 'Three' after index 5
       System.out.println("The first occurrences of element 'Three' from index 5 is at index"+vec.indexOf("Three",4));
       // Trying to find the index of a non exitent element
       // The function should return -1
       System.out.println("The first occurrences 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 first occurrences of element 'Three' from index 5 is at index 5
The first occurrences of element 'Twelve' is at -1

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: