Java Program to Search an Element in Vector
Last Updated :
21 Jun, 2024
Improve
A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector using Java.
Methods to Search an Element in Vector
- Using indexOf Method
- Using for Loop
Approach 1: Using indexOf Method
Declaration:
public int indexOf(Object o)
Return Value: It returns the index of the first occurrence of a specified element in the vector.
Java Program to Search an Element in a Vector Using indexOf Method
Below is the implementation to Search an Element in Vector using Java:
// Java Program to Search an Element in Vector using indexOf method
// Importing required classes
import java.util.Vector;
// Main Class
public class VectorSearch {
// Main driver method
public static void main(String[] args) {
// Creating a Vector and adding elements
Vector<String> vector = new Vector<>();
vector.add("GeeksforGeeks");
vector.add("Data Structures");
vector.add("Algorithms");
vector.add("Operating Systems");
// Element to be searched
String searchElement = "Algorithms";
// Searching for the element using indexOf method
int index = vector.indexOf(searchElement);
// Checking if the element is present and printing the result
if (index != -1) {
System.out.println(searchElement + " found at index " + index);
} else {
System.out.println(searchElement + " not found in the vector");
}
}
}
Output
Algorithms found at index 2
Explanation of the above Program:
- The program initializes a Vector and adds elements to it.
- The indexOf method is used to find the index of the specified element.
- It checks if the element is found (index not equal to -1).
- Prints the position of the element if found, otherwise indicates it is not found.
Complexity of the Above Method:
- Time Complexity: O(n), where n is the number of elements in the vector.
- Auxiliary Space: O(1), as no extra space is used.
Approach 2: Using for Loop
Declaration:
for (initialization expr; test expr; update exp)
{
// body of the loop
// statements we want to execute
}
Java Program to Search an Element in a Vector Using for Loop
Below is the implementation to Search an Element in Vector using Java:
// Java Program to Search an Element in Vector using for loop
// Importing required classes
import java.util.Vector;
// Main Class
public class VectorSearch {
// Main driver method
public static void main(String[] args) {
// Creating a Vector and adding elements
Vector<String> vector = new Vector<>();
vector.add("GeeksforGeeks");
vector.add("Data Structures");
vector.add("Algorithms");
vector.add("Operating Systems");
// Element to be searched
String searchElement = "Algorithms";
// Searching for the element using enhanced for loop
boolean isFound = false;
int index = -1;
for (int i = 0; i < vector.size(); i++) {
if (vector.get(i).equals(searchElement)) {
isFound = true;
index = i;
break;
}
}
// Checking if the element is present and printing the result
if (isFound) {
System.out.println(searchElement + " found at index " + index);
} else {
System.out.println(searchElement + " not found in the vector");
}
}
}
Output
Algorithms found at index 2
Explanation of the above Program:
- We iterate over the each element of the Vector using the for Loop.
- When we find the match, then we set the isFound flag value as true and record the index.
- If the element is found then we print the index of the element, else not found.
Complexity of the Above Method:
- Time Complexity: O(n), where n is the number of elements in the vector.
- Auxiliary Space: O(1), as no extra space is used.