- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Vector lastIndexOf() Method
Description
The Java Vector lastIndexOf(Object) method returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
Declaration
Following is the declaration for java.util.Vector.lastIndexOf() method
public int lastIndexOf(Object o)
Parameters
o − The element to search for.
Return Value
This method returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
Exception
NA
Java Vector lastIndexOf(Object elem,int index) Method
Description
This is another variant of lastIndexOf() method.It searches backwards for the specified object, starting from the specified index, and returns an index to it.
Declaration
Following is the declaration for java.util.Vector.lastIndexOf() method
public int lastIndexOf(Object elem,int index)
Parameters
elem − This is the desired component
index − This is the index to start searching from.
Return Value
The method call returns the index of the last occurrence of the specified object in this vector at position less than or equal to index in the vector.
Exception
IndexOutOfBoundsException − This exception is thrown if index is greater than or equal to the current size of this vector.
Getting Last Index of an Element of a Vector of Integer Example
The following example shows the usage of Java Vector lastIndexOf(object) method. We're creating a Vector of Integers. We're adding couple of Integers to the Vector object using add() method calls per element and using lastIndexOf(object) method, we're checking last index of an element and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Integer> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(0); vector.add(1); vector.add(2); vector.add(5); vector.add(4); vector.add(5); vector.add(6); // let us print last index of 5 System.out.println("Last index of 5 = " + vector.lastIndexOf(5)); } }
Output
Let us compile and run the above program, this will produce the following result −
Last index of 5 = 5
Getting Last Index of an Element of a Vector of Integer from a Given Index Example
The following example shows the usage of Java Vector lastIndexOf(object, index) method. We're creating a Vector of Integers. We're adding couple of Integers to the Vector object using add() method calls per element and using lastIndexOf(object, index) method, we're checking last index of an element starting from given index and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Integer> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(0); vector.add(1); vector.add(2); vector.add(5); vector.add(4); vector.add(5); vector.add(6); // let us print last index of 5 System.out.println("Last index of 5 = " + vector.lastIndexOf(5, 3)); } }
Output
Let us compile and run the above program, this will produce the following result −
Last index of 5 = 3
Getting Last Element of a Vector of String Example
The following example shows the usage of Java Vector lastIndexOf(object) method. We're creating a Vector of Strings. We're adding couple of Strings to the Vector object using add() method calls per element and using lastIndexOf(object) method, we're checking last index of an element and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<String> vector = new Vector<>(); // use add() method to add elements in the vector vector.add("A"); vector.add("B"); vector.add("C"); vector.add("B"); // let us print index of B System.out.println("Last index of B = " + vector.lastIndexOf("B")); } }
Output
Let us compile and run the above program, this will produce the following result −
Last index of B = 3
Getting Last Element of a Vector of Object Example
The following example shows the usage of Java Vector lastIndexOf(object) method. We're creating a Vector of Student objects. We're adding couple of Student objects to the Vector object using add() method calls per element and using lastIndexOf(object) method, we're checking last index of an element and printing it.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Student> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(new Student(1, "Julie")); vector.add(new Student(2, "Robert")); vector.add(new Student(3, "Adam")); vector.add(new Student(4, "Adam")); // let us print last index of Adam System.out.println("Last index of Adam = " + vector.lastIndexOf(new Student(4, "Adam"))); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
Output
Let us compile and run the above program, this will produce the following result −
Last index of Adam = 3