The indexOf(Object elem) method is used to search the first occurence of the given argument.The equality is tested by using the equals method.
DeclarationFollowing is the declaration for java.util.Vector.indexOf() method
public int indexOf(Object elem)Parameters
elem − The input parameter is an object
Return ValueThe return value is the index of the first occurrence of the argument in this vector.It returns -1 if the object is not found.
ExceptionNA
Java Vector indexOf(Object elem,int index) Method DescriptionThis is another variant of previous indexOf() method.The only difference is that the search for the first occurence of the given argument starts at the index position mentioned as the second parameter.
DeclarationFollowing is the declaration for java.util.Vector.indexOf() method
public int indexOf(Object elem,int index)Parameters
elem − The input parameter is an object
index − This is the non-negative index to start searching from
The method call returns the index of the first occurrence of the object argument in this vector at position index or later in the vector.
ExceptionIndexOutOfBoundsException − This exception is thrown if the index is negative
Getting Index of an Element of a Vector of Integer ExampleThe following example shows the usage of Java Vector indexOf(Object element) method to get the index of an element in this vector. We're adding couple of Integers to the Vector object using add() method calls per element and using indexOf(Object element) method, we're getting index of 3 and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<>(4); // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(3); // let us get the index of 3 System.out.println("Index of 3 is: "+vec.indexOf(3)); } }Output
Let us compile and run the above program, this will produce the following result. Please note the index of first occurrence of 3 is 1 so 1 is printed instead of 3.
Index of 3 is: 1Getting Index of an Element of a Vector of Integer Starting from Other Index Example
The following example shows the usage of Java Vector indexOf(Object element, Int index) method to get the index of an element in this vector starting from given index. We're adding couple of Integers to the Vector object using add() method calls per element and using indexOf() method, we're getting index of 3 and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<>(4); // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(3); vec.add(2); vec.add(3); /** let us get the index of 3 * starting search from 2nd index */ System.out.println("Index of 3 :"+vec.indexOf(3,2)); } }Output
Let us compile and run the above program, this will produce the following result.
Index of 3 :3Getting Index of an Element of a Vector of String Example
The following example shows the usage of Java Vector indexOf(Object element) method to get the index of an element in this vector. We're adding couple of String to the Vector object using add() method calls per element and using indexOf(Object element) method, we're getting index of 3 and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<String> vec = new Vector<>(4); // use add() method to add elements in the vector vec.add("D"); vec.add("C"); vec.add("A"); vec.add("C"); // let us get the index of C System.out.println("Index of C is: "+vec.indexOf("C")); } }Output
Let us compile and run the above program, this will produce the following result. Please note the index of first occurrence of C is 1 so 1 is printed instead of C.
Index of C is: 1
java_util_vector.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4