Java Program to Search an Element in Vector Last Updated : 21 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 VectorUsing indexOf MethodUsing for LoopApproach 1: Using indexOf MethodDeclaration: 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 MethodBelow is the implementation to Search an Element in Vector using Java: 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"); } } } OutputAlgorithms 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 LoopDeclaration: 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 LoopBelow is the implementation to Search an Element in Vector using Java: 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"); } } } OutputAlgorithms 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. Comment More infoAdvertise with us Next Article Java Program to Search an Element in a Linked List A anjalibo6rb0 Follow Improve Article Tags : Java Java Programs Java-Vector Practice Tags : Java Similar Reads Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Sort and Search an Element in Java In Java sorting and searching an element in an array is very easy. Unlike C, where we have to make all the functions to work, Java has inbuilt functions to do the same work. To sort an array there is a sort function and to search an element in a sorted array there is a binarySearch() function. To le 3 min read Java Program to Get the Maximum Element From a Vector Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l 4 min read Replacing Element in Java Vector To replace an element in Java Vector, set() method of java.util.Vector class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element. The index of a Vector is zero-based. So, to replace the first element, 0 should be the index passed 2 min read Java Program to Copy Elements of ArrayList to Vector Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collectio 4 min read Java Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi 4 min read Like