How to Search an Vector Element from Specific Index in Java


The example aims to show how to search an element of Vector from specific index by indexOf and lastIndexOf methods in Java.

Source Code

package com.beginner.examples;

import java.util.Vector;

public class SearchAnElementFromSpecificIndexVector {

	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 search an index of specified element in Vector from specified index us indexOf method.
		 */
		int index = vector.indexOf("A",0);
		if(index == -1) {
			System.out.println("Vector does not contain A after index 4");
		}  
		else {
			System.out.println("Vector contains A after index 4 at index" + index);
		}
	      
	}

}

Output:

Vector contains A after index 4 at index0

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments