Java.util.Stack 类

java.util.Stack.lastIndexOf() 方法返回此堆栈中指定元素最后一次出现的索引,从指定索引向后搜索。如果堆栈中不存在元素,则返回 -1。

语法

public int lastIndexOf(Object o, int index)

参数

o 指定要在堆栈中搜索的元素。
index 指定索引到search 向后搜索。

返回值

返回指定元素在栈中最后一次出现的索引,向后搜索从指定索引开始,如果元素不存在于堆栈中,则返回 -1。

异常

无。

示例:

In下面的示例中,java.util.Stack.lastIndexOf()方法用于查找堆栈MyStack,从指定索引处向后搜索。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建堆栈
    Stack<Integer> MyStack = new Stack<Integer>();

    //填充堆栈
    MyStack.push(10);
    MyStack.push(30);
    MyStack.push(20);
    MyStack.push(50);
    MyStack.push(30);

    System.out.print("30 appeared last time before index=2 at index="); 
    System.out.print(MyStack.lastIndexOf(30, 2));
  }
}

上述代码的输出将是:

30 appeared last time before index=2 at index=1