Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector lastIndexOf(Object elem, int index) Method

Java Vector lastIndexOf(Object elem, int index) Method

Java Vector Java Vector


lastIndexOf(Object elem, int index) is a method in Java's Vector An important method provided by the class, used to search for a specified element backward within a specified range. This method can help developers efficiently find the position of a specific element in a collection.

Method syntax:



public int lastIndexOf(Object o, int index)

Parameter Description

  1. Object o - The element to find in the Vector

  2. can be any type of object
  3. If the Vector contains null elements, null can also be searched for
  4. - Can be an object of any type - If the Vector contains null elements, null can also be searched for
    1. int index - The starting index position for the search

    2. The search proceeds forward from this index (toward the beginning of the Vector)
    3. This index is included in the search range
    4. - The search proceeds forward from this index (toward the beginning of the Vector) - This index is included in the search range

      Notes

      Return Value

      Return value type

      Meaning of the Return Value

      1. If the element is found: returns the last index position of the element in the Vector (searching forward from the specified position)
      2. If the element is not found: returns -1
      3. If the argument index is out of range: throws IndexOutOfBoundsException

      Method Example

      Basic Usage Example

      Example

      import

      java.util.Vector

      ;

      public

      class

      VectorExample

      {

         

      public

      static

      void

      main

      (

      String

      [

      ]

      args

      )

      {

             

      // Create a Vector and add elements

              Vector

      <
      
      String
      
      fruits
      
      =
      
      new
      
      Vector
      
      <>
      
      (
      
      )

      ;

              fruits.

      add

      (

      "Apple"

      )

      ;

              fruits.

      add

      (

      "Banana"

      )

      ;

              fruits.

      add

      (

      "Orange"

      )

      ;

              fruits.

      add

      (

      "Apple"

      )

      ;

              fruits.

      add

      (

      "Grape"

      )

      ;

             

             

      // Search backward for "Apple" starting from index 3

             

      int

      index

      =

      fruits.

      lastIndexOf

      (

      "Apple"

      ,

      3

      )

      ;

             

      System

      .

      out

      .

      println

      (

      "Last index of 'Apple' from index 3: "

      index

      )

      ;

             

             

      // Search backward for "Banana" starting from index 2

              index

      =

      fruits.

      lastIndexOf

      (

      "Banana"

      ,

      2

      )

      ;

             

      System

      .

      out

      .

      println

      (

      "Last index of 'Banana' from index 2: "

      index

      )

      ;

             

             

      // Search for a nonexistent element

              index

      =

      fruits.

      lastIndexOf

      (

      "Peach"

      ,

      4

      )

      ;

             

      System

      .

      out

      .

      println

      (

      "Last index of 'Peach' from index 4: "

      index

      )

      ;

         

      }

      }

      Output result

      Last index of 'Apple' from index 3: 3
      
      Last index of 'Banana' from index 2: 1
      
      Last index of 'Peach' from index 4: -1
      
      

      Method implementation principle

      Underlying Implementation

      Vector The class internally uses an array to store elements, and thelastIndexOf(Object o, int index) method's implementation essentially traverses the array forward from the specified index position until it finds a matching element or reaches the beginning of the array.

      Time Complexity

      Comparison with Similar Methods

      1. The difference between lastIndexOf(Object o) differ as follows:

      2. lastIndexOf(Object o) Searches the entire Vector
      3. lastIndexOf(Object o, int index) Searches only from the specified index to the beginning of the Vector
      4. - `lastIndexOf(Object o)` searches the entire Vector - `lastIndexOf(Object o, int index)` searches only from the specified index to the beginning of the Vector
        1. The difference between indexOf(Object o, int index) differ as follows:

        2. indexOf searches backward from the specified position
        3. lastIndexOf searches forward from the specified position
        4. - `indexOf` searches backward from the specified position - `lastIndexOf` searches forward from the specified position

          Use Cases

          Applicable Situations

          1. When you need to find the last occurrence of an element within a specific range of the Vector
          2. When you need to search forward from a known position
          3. When processing large Vectors and knowing that the target element is in a certain region, search efficiency can be improved

          Practical Application Examples

          Example

          // Find the last occurrence of a specific error message in log analysis

          Vector

          <
          
          String
          
          logEntries
          
          =
          
          getLogEntries
          
          (
          
          )

          ;

          int

          lastErrorIndex

          =

          logEntries.

          lastIndexOf

          (

          "ERROR"

          , logEntries.

          size

          (

          )

          1

          )

          ;

          // Find the player's most recent position record in game development

          Vector

          <
          
          PlayerPosition
          
          positionHistory
          
          =
          
          getPositionHistory
          
          (
          
          )

          ;

          int

          lastCheckpointIndex

          =

          positionHistory.

          lastIndexOf

          (

             

          new

          PlayerPosition

          (

          checkpointX, checkpointY

          )

          ,

              currentPositionIndex

          )

          ;


          Notes

          Boundary Condition Handling

          1. Null value handling:

          2. Vector can contain null elements
          3. Can search for null:lastIndexOf(null, index)
          4. - Vector can contain null elements - Can search for null: `lastIndexOf(null, index)`
            1. Index out of bounds:

            2. If index >= size(), the entire Vector is searched
            3. If index < 0, throws IndexOutOfBoundsException
            4. - If index >= size(), the entire Vector is searched - If index < 0, throws IndexOutOfBoundsException

              Performance considerations

              Thread Safety


              Summary

              Vector.lastIndexOf(Object elem, int index) is a practical method that provides the ability to search for elements forward from a specified position. Understanding how this method works and its applicable scenarios can help developers handle collection search requirements more efficiently. Remember to handle boundary conditions properly and consider alternatives in performance-sensitive scenarios.

              Java Vector Java Vector