Vector indexOf() Method in Java
The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element.
Syntax:
Vector.indexOf(Object element)
Parameters: This method accepts a mandatory parameter element of the type of Vector. It specifies the element whose occurrence is needed to be checked in the Vector.
Return Value: This method returns the index or position of the first occurrence of the element in the vector. Else it returns -1 if the element is not present in the vector. The returned value is of integer type.
Below programs illustrate the Java.util.Vector.indexOf() method:
Program 1:
filter_none
edit
close{#close-code-editor-button}
play_arrow{#run-code-button}
link
brightness_4
code {#edit-on-ide-button}
// Java code to illustrate indexOf() import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<String> vec_tor = new Vector<String>(); // Use add() method to add elements in the Vector vec_tor.add( "Geeks" ); vec_tor.add( "for" ); vec_tor.add( "Geeks" ); vec_tor.add( "10" ); vec_tor.add( "20" ); // Displaying the Vector System.out.println( "Vector: " + vec_tor); // The first position of an element // is returned System.out.println( "The first occurrence of Geeks is at index:" + vec_tor.indexOf( "Geeks" )); System.out.println( "The first occurrence of 10 is at index: " + vec_tor.indexOf( "10" )); } } |
chevron_right
filter_none
Output:
Vector: [Geeks, for, Geeks, 10, 20]
The first occurrence of Geeks is at index:0
The first occurrence of 10 is at index: 3
Program 2:
filter_none
edit
close{#close-code-editor-button}
play_arrow{#run-code-button}
link
brightness_4
code {#edit-on-ide-button}
// Java code to illustrate indexOf() import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<Integer> vec_tor = new Vector<Integer>(); // Use add() method to add elements in the Vector vec_tor.add( 1 ); vec_tor.add( 2 ); vec_tor.add( 3 ); vec_tor.add( 10 ); vec_tor.add( 20 ); // Displaying the Vector System.out.println( "Vector: " + vec_tor); // The first position of an element // is returned System.out.println( "The first occurrence of Geeks is at index:" + vec_tor.indexOf( 2 )); System.out.println( "The first occurrence of 10 is at index: " + vec_tor.indexOf( 20 )); } } |
chevron_right
filter_none
Output:
Vector: [1, 2, 3, 10, 20]
The first occurrence of Geeks is at index:1
The first occurrence of 10 is at index: 4
My Personal Notes arrow_drop_up
Save
Recommended Posts:
- LinkedList indexOf() method in Java
- CopyOnWriteArrayList indexOf() method in Java
- AbstractSequentialList indexOf() method in Java with Example
- Stack indexOf() method in Java with Example
- Stack indexOf(Object, int) method in Java with Example
- StringBuilder indexOf() method in Java with Examples
- AbstractList indexOf() method in Java with Examples
- List indexOf() Method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
- Java Guava | Ints.indexOf(int[] array, int[] target) method with Examples
- Vector get() Method in Java
- Vector set() Method in Java
- Vector contains() Method in Java
- Vector add() Method in Java
- Java Guava | Floats.indexOf(float[] array, float target) method with Examples
kundankumarjha
Check out this Author's contributed articles.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Article Tags :
Java Java - util package
Java-Collections
Java-Functions
Java-Vector
Practice Tags :
Java
Java-Collections
thumb_up
Be the First to upvote.
To-do Done
0
No votes yet.
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Post navigation
Previous
first_page Vector elementAt() Method in Java Next
last_page Vector copyInto() Method in Java
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments