How to Search Vector Elements in Java


Here we may learn how to search an element of Vector using binarySearch method of Collections class in Java.

Source Code

package com.beginner.examples;

import java.util.Collections;
import java.util.Vector;

public class BinarySearchOfVectorExample {

	
	public static void main(String[] args) {
		Vector vector = new Vector();
		
		vector.add("a");
		vector.add("b");
		vector.add("c");
		vector.add("d");
		vector.add("e");
		vector.add("f");
		
		//Use the binarySearch() method in the Collections tools class
		int index =  Collections.binarySearch(vector, "c");
		
		System.out.println("c---->"+index);

	}

}

Output:

c---->2

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments