当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Stack lastIndexOf(Object, int)用法及代码示例


Java.util.Stack.lastIndexOf(Object element,int last_index)方法用于在此Stack中首次出现指定元素的最后一个索引,从最后一个索引开始向前搜索,如果未找到该元素,则返回-1 。更正式地,返回最低的最后一个索引i,使得(i> =最后一个索引&& Objects.equals(o,get(i)));如果没有这样的最后一个索引,则返回-1。

用法:

public int lastIndexOf(Object element, 
                        int last_index)

参数:此方法接受两个参数:


  • element类型的堆栈。它指定需要在堆栈中检查其出现的元素。
  • 最后索引类型为Integer。它指定了要开始搜索的最后一个索引

返回值:此方法从指定的最后一个索引返回堆栈中元素的第一个匹配项的最后一个索引或位置。否则,如果元素在堆栈中不存在,则返回-1。返回值是整数类型。

异常:如果指定的索引大于或等于此向量的当前大小,则此方法引发IndexOutOfBoundsException

以下示例程序旨在说明Java.util.Stack.lastIndexOf()方法:

程序1:

// Java code to illustrate lastIndexOf() 
  
import java.util.*; 
  
public class StackDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty Stack 
        Stack<String> stack = new Stack<String>(); 
  
        // Use add() method to add elements in the Stack 
        stack.add("Geeks"); 
        stack.add("for"); 
        stack.add("Geeks"); 
        stack.add("10"); 
        stack.add("Geeks"); 
  
        // Displaying the Stack 
        System.out.println("Stack:" + stack); 
  
        // The first position of an element 
        // is returned 
        System.out.println("The first occurrence"
                           + " of Geeks is at last index:"
                           + stack.indexOf("Geeks")); 
  
        // Get the last occurrence of Geeks 
        // using lastIndexOf() method 
        System.out.println("The last occurrence"
                           + " of Geeks is at last index:"
                           + stack 
                                 .lastIndexOf("Geeks", 
                                              stack.lastIndexOf("Geeks"))); 
    } 
}
输出:
Stack:[Geeks, for, Geeks, 10, Geeks]
The first occurrence of Geeks is at last index:0
The last occurrence of Geeks is at last index:4

程序2:演示IndexOutOfBoundsException

// Java code to illustrate lastIndexOf() 
import java.util.*; 
  
public class StackDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty Stack 
        Stack<Integer> stack = new Stack<Integer>(); 
  
        // Use add() method to add elements in the Stack 
        stack.add(1); 
        stack.add(2); 
        stack.add(3); 
        stack.add(10); 
        stack.add(20); 
  
        // Displaying the Stack 
        System.out.println("Stack:" + stack); 
  
        // Get the 10th occurrence of Geeks 
        // using lastIndexOf() method 
        System.out.println("The 10th occurrence"
                           + " of Geeks is at index:"); 
  
        try { 
            stack.lastIndexOf("Geeks", 10); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
Stack:[1, 2, 3, 10, 20]
The 10th occurrence of Geeks is at index:
java.lang.IndexOutOfBoundsException:10 >= 5


相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Stack lastIndexOf(Object, int) method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。