Home » Java programming language

Java Vector lastIndexOf() Method with Example

Vector Class lastIndexOf() method: Here, we are going to learn about the lastIndexOf() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 19, 2020

Vector Class lastIndexOf() method

Syntax:

    public int lastIndexOf (Object ob);
    public int lastIndexOf (Object ob, int indices);
  • lastIndexOf() method is available in java.util package.
  • lastIndexOf(Object ob) method is used to return the index of the last occurrence of the given element.
  • lastIndexOf(Object ob, int indices) method is used to find the index of the last occurrence of the given object in this Vector and searching starts at the given indices.
  • These methods may throw an exception at the time of returning an index.
    IndexOutOfBoundsException: This exception may throw when the given parameter is not in a range.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, lastIndexOf(Object ob)
    • Object ob – represents the object for which the last occurrence element index to be returned.
  • In the first case, lastIndexOf (Object ob, int indices)
    • Object ob – represents the object for which the last occurrence element index to be returned.
    • int indices – represents the index of searching starts.

Return value:

In both the cases, the return type of the method is int - it gets the index of the last occurrence of the given object when exists otherwise it returns -1.

Example:

// Java program to demonstrate the example 
// of lastIndexOf() method of Vector

import java.util.*;

public class LastIndexOfVector {
 public static void main(String[] args) {
  // Instantiates a vector object     
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add
  // the elements in vector
  v.add("C");
  v.add("C++");
  v.add("SFDC");
  v.add("C++");
  v.add("JAVA");

  //Display Vector
  System.out.println("v: " + v);

  // By using lastIndexOf(object) method is used
  // to return the index of last occurrence of the
  // given object
  System.out.println("v.lastIndexOf(C++): " + v.lastIndexOf("C++"));

  // By using lastIndexOf(object, indices) method is used
  // to return the index of last occurrence of the
  // given object and searching starts from the 
  // given indices
  System.out.println("v.lastIndexOf(C++,4): " + v.lastIndexOf("C++", 4));
 }
}

Output

v: [C, C++, SFDC, C++, JAVA]
v.lastIndexOf(C++): 3
v.lastIndexOf(C++,4): 3





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.