Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Stack.lastIndexOf(element, index) method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Stack class is a Last-In-First-Out (LIFO)The element inserted first is processed last and the element inserted last is processed first. stack of objects.

The lastIndexOf method of the Stack class will search for the specific element backwards from the specific index and return the first index at which the searching element is found.

Syntax

public int lastIndexOf(Object obj, int index);

Parameter

This method takes 2 arguments.

  1. obj: The element to be searched.

  2. index: The index from which the element is to be searched backward.

Return value

When searching for the passed element backwards from the specific index, the first index at which the searching element is found is returned.

If the element is not present from the specified index, then -1 is returned.

Code

The code below demonstrates how to use the lastIndexOf method.

import java.util.Stack;
class LastIndexOfExample {
    public static void main( String args[] ) {
      Stack<String> stack = new Stack<>();
      stack.push("one");
      stack.push("two");
      stack.push("two");
      stack.push("three");
      System.out.println("The stack is " + stack);

      System.out.println("Last Index of element 'two' from index 3 : " + stack.lastIndexOf("two", 3));
      System.out.println("Last Index of element 'four' from index 3 : " + stack.lastIndexOf("four", 3));
    }
}

Explanation

In the code above:

  • In line number 1, we import the Stack class.

  • In line number 4, we create a new object for Stack object with the name stack.

  • From line numbers 5 to 8, we use the push method of the stack object to add four elements ("one","two", "two", "three") to the stack.

  • In line number 11, we use the lastIndexOf method of the stack object with the searching element as two and 3 as the index. This method will search backwards for the element two from index 3. The element two is present in two indices: 1 and 2. We get 2 as a result, since that is the first occurrence from index 3 when searching backwards.

  • In line number 12, we use the lastIndexOf method of the stack object with the searching element as four and 3 as the index. This method will search backwards for the element two from index 3. The element four is not present in the stack so -1 is returned.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring