Java Vector lastIndexOf() MethodThe lastIndexOf() Java Vector class method is used to get the index of the last occurrence of the specified element in the vector. There are two different types of Java lastIndexOf() method which can be differentiated depending on its parameter. These are:
lastIndexOf(Object o) Method:It returns the index of the last occurrence of the specified element in this vector. If the element is not found, it returns -1. lastIndexOf(Object o, int index> c) Method:This method is used to get the index of the last occurrence of the specified element in this vector. It starts searching for an element in the backward direction from the specified index. If the element is not found, it returns -1. SyntaxFollowing is the declaration of lastIndexOf() method: Parameter
ReturnThe lastIndexOf() method returns the index of the last 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 >= size()). Compatibility VersionJava 1.2 and above Example 1Test it NowOutput: Index of element is: 3 Example 2Test it NowOutput: Index of element is: -1 The element is not found. Example 3Test it NowOutput: Exception in thread "main" java.lang.IndexOutOfBoundsException: 6 >= 5 at java.base/java.util.Vector.lastIndexOf(Vector.java:469) at myPackage.VectorLastIndexOfExample3.main(VectorLastIndexOfExample3.java:14) Example 4Test it NowOutput: Index of element is found at: 1 Next TopicJava Vector |
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