In Java, lastIndexOf(o)scans a Stack from the top down and returns the index of the first element equal to o that it meets: the copy closest to the top. The index itself is still counted from the bottom, starting at 0. No match gives -1.
Stack's own search() is built on this method: search(o) equals size() - lastIndexOf(o) whenever the element exists. The overload lastIndexOf(o, index) starts the downward scan at a given index.
An instance method inherited from java.util.Vector (since Java 1.0). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.
Syntax
Pass the value to find, and optionally the index where the downward scan starts.
Java
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("a");
stack.push("c"); // [a, b, a, c]
int last = stack.lastIndexOf("a"); // 2
int first = stack.indexOf("a"); // 0
int below = stack.lastIndexOf("a", 1); // 0 (scan from index 1 down)
int none = stack.lastIndexOf("z"); // -1
Call
What it does
stack.lastIndexOf(o)
Index of the match closest to the top, or -1.
stack.lastIndexOf(o, i)
The same, scanning down from index i.
stack.size() - stack.lastIndexOf(o)
The value search(o) returns when o is present.
stack.indexOf(o)
The match closest to the bottom instead.
Like indexOf, the scan is linear, but it finishes quickly when the match is near the top.
Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized int lastIndexOf(Object o)
public synchronized int lastIndexOf(Object o, int index)
Parameters
Parameter
Description
o (Object)
The value to find, compared with equals(); null is allowed.
index (int)
Only in the two-argument form: where the downward scan starts.
Return Value
Return Value
Description
int
The 0-based index (from the bottom) of the match closest to the top, or -1.
Recreate search by hand. For each tab, the loop computes size() - lastIndexOf(n), or -1 when the tab is missing.
Duplicates resolved the same way. mail appears at indexes 0 and 2; both the formula and search use index 2, giving a distance of 3.
Identical results. All three rows match search exactly, including -1 for maps, because that is how Stack.search is written.
Example 3 — Walk Back Through Matches with lastIndexOf(o, from)
List every visit to the home page, newest first.
Java
import java.util.Stack;
public class StackLastIndexOfFrom {
public static void main(String[] args) {
Stack<String> visits = new Stack<>();
visits.push("home");
visits.push("shop");
visits.push("home");
visits.push("blog");
visits.push("home");
int i = visits.lastIndexOf("home");
while (i != -1) {
System.out.println("home at index " + i);
i = visits.lastIndexOf("home", i - 1);
}
}
}
Output
home at index 4
home at index 2
home at index 0
How It Works
Newest visit first. The one-argument call returns 4, the home visit at the top.
Keep scanning down. Each lastIndexOf("home", i - 1) resumes just below the previous match, finding 2 and then 0.
A negative start ends the loop. After index 0 the next start is -1; the method returns -1 for that instead of throwing, which stops the while loop.
Frequently Asked Questions
The index of the matching element closest to the top, counted from the bottom starting at 0, or -1 if there is no match.
search(o) returns size() - lastIndexOf(o) when o is present and -1 otherwise. Both pick the copy nearest the top.
indexOf() returns the match nearest the bottom; lastIndexOf() returns the match nearest the top. Without duplicates both give the same index.
The one-argument version cannot. lastIndexOf(o, index) throws IndexOutOfBoundsException if index is greater than or equal to size().
equals(), so equal values match even if they are different objects. A null argument is matched with ==.