How to Search an Element of Java Vector


In this example we will show how to search an element of java Vector object with contains, indexOf and lastIndexOf methods.

Source Code

package com.beginner.examples;

import java.util.Vector;

public class SearchAnElementInVector {

	public static void main(String[] args) {
		
		//create Vector.
		Vector vector = new Vector();
	    
		vector.add("A");
		vector.add("B");
		vector.add("C");
		vector.add("D");
		
		/*
		 * To check whether the specified element exists in Java Vector use contains method.
		 */
		boolean result = vector.contains("C");
		System.out.println("Does Vector contain C ? " + result);

	}

}

Output:

Does Vector contain C ? true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments