當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。