Java Vector indexOf() Method24 Mar 2025 | 3 min read The indexOf() Java Vector class method is used to get the index of the first occurrence of the specified element in the vector. There are two different types of Java indexOf() method which can be differentiated depending on its parameter. These are:
indexOf(Object o) Method:This method is used to get the index of the first occurrence of the specified element in the vector. If the element is not found, it returns -1. indexOf(Object o, int index> c) Method:This method is used to get the index of the first occurrence of the specified element in the vector. It starts searching for an element in the forward direction from the specified index. If the element is not found, it returns -1. SyntaxFollowing is the declaration of indexOf() method: Parameter
ReturnThe indexOf() method returns the index of the first occurrence of the specified element in this vector or returns -1 if the vector does not contain the element. ExceptionsIndexOutOfBoundsException- This method has thrown an exception if the index of an array is out of range i.e. (index < 0 || index > size()). Compatibility VersionJava 1.2 and above Example 1Output: Index of element is: 2 Example 2Output: Index of element is: -1 The element is not found. Example 3Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2 at java.base/java.util.Vector.indexOf(Vector.java:430) at myPackage.VectorIndexOfExample3.main(VectorIndexOfExample3.java:14) Example 4Output: Index of element is found at: 3 Next TopicJava Vector |
Java Method The insertElementAt() method of Java Vector class is used to insert the specified object as a component in this vector at the specified index. Each component in the vector with an index greater or equal to the given index is shifted upward. Now,...
3 min read
Java Method The toArray() Java Vector class method is used to get an array containing all of the elements in this vector in the correct order. There is two different types of Java toArray() method which can be differentiated depending on its parameter. These are: Java...
3 min read
Java Method The removeRange() method of Java Vector class is used to delete all elements from the vector whose index is between fromIndex, inclusive and toIndex, exclusive. The removal of elements shifts any succeeding elements to the left (reduces their index). Syntax Following is the declaration of...
2 min read
Java Method The sort() method of Java Vector class is used to sort the vector according to the order induced by the specified Comparator. Syntax Following is the declaration of a sort() method: Public void sort(Comparator<? super E> c) Parameter Parameter Description Required/Optional c It is the Comparator which is used to compare vector...
3 min read
Java Method The removeElementAt() method of Java Vector class is used to delete the component at the specified index. Each element in this vector with an index greater or equal to the given index is shifted downward. Now the vector index is one smaller than...
2 min read
Java Method The set() method of Java Vector class is used to replace the element at the specified position in the vector with the specified element. Syntax Following is the declaration of set() method: Public E set(int index, E element) Parameter Parameter Description Required/Optional index It is an index of the element which will...
2 min read
Java Method The get() method of Java Vector class is used to get the element at the specified position in the vector. Syntax Following is the declaration of get() method: public E get(int index) Parameter DataType Parameter Description Required/Optional int index It is an index of the element which will be returned. Required Return The get() method returns the...
2 min read
Java Method The setElementAt() method of Java Vector class is used to replace the component at the specified index of this vector to be the specified object. Syntax Following is the declaration of setElementAt() method: public void setElementAt(E obj, int index) Parameter Parameter Description Required/Optional index It is the specified index where an element...
2 min read
Java Method The clone() method of Java Vector class is used to get a clone of the vector which is in use. Syntax: Following is the declaration of clone() method: Public object clone() Parameter: This method does not accept any parameter. Returns: This method returns a clone of the vector which is...
2 min read
Java Method The addElement() method of Java Vector class is used to add the specified element to the end of this vector. Adding an element increases the vector size by one. Syntax: Following is the declaration of addElement() method: Public void addElement(E e) Parameter: Parameter Description Required/Optional e It is the element which will...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India