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


Java Stack subList()用法及代碼示例


Java.util.Stack類的subList()方法用於返回此Stack部分在指定的fromIndex(包括)和toIndex(不包括)之間的視圖。 (如果fromIndex和toIndex相等,則返回的Stack為空。)

返回的堆棧由該堆棧支持,因此返回的堆棧中的非結構更改會反映在此堆棧中,反之亦然。返回的堆棧支持所有可選的堆棧操作。

用法:


public Stack subList(int fromIndex, int toIndex)

參數:此方法將以下參數作為參數。

  • fromIndex –subList的低端點(包括端點)
  • toIndex –subList的高端端點(不包括)

返回值:此方法返回此Stack中指定範圍的視圖。

異常:此方法引發以下異常。

  • IndexOutOfBoundsException –如果端點索引值超出範圍(fromIndex大小)
  • IllegalArgumentException –如果端點索引不正確(fromIndex> toIndex)

以下示例說明了subList()方法。

示例1:

// Java program to demonstrate 
// subList() method 
// for String value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
  
            // Creating object of Stack<Integer> 
            Stack<String> 
                stack = new Stack<String>(); 
  
            // Populating stack1 
            stack.add("A"); 
            stack.add("B"); 
            stack.add("C"); 
            stack.add("D"); 
            stack.add("E"); 
  
            // print stack 
            System.out.println("Original stack: "
                               + stack); 
  
            // getting the subList 
            // using subList() method 
            List<String> stack2 = stack.subList(2, 4); 
  
            // print the subList 
            System.out.println("SubStack of stack: "
                               + stack2); 
        } 
  
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
Original stack: [A, B, C, D, E]
SubStack of stack: [C, D]

示例2:對於IndexOutOfBoundsException

// Java program to demonstrate 
// subList() method 
// for IndexOutOfBoundsException 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
        try { 
  
            // Creating object of Stack<Integer> 
            Stack<String> 
                stack = new Stack<String>(); 
  
            // Populating stack1 
            stack.add("A"); 
            stack.add("B"); 
            stack.add("C"); 
            stack.add("D"); 
            stack.add("E"); 
  
            // print stack 
            System.out.println("Original stack: "
                               + stack); 
  
            // getting the subList 
            // using subList() method 
            System.out.println("\nEnd index value is out of range"); 
            List<String> stack2 = stack.subList(2, 7); 
  
            // print the subList 
            System.out.println("SubStack of stack: "
                               + stack2); 
        } 
  
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
Original stack: [A, B, C, D, E]

End index value is out of range
java.lang.IndexOutOfBoundsException: toIndex = 7

示例3:對於IllegalArgumentException

// Java program to demonstrate 
// subList() method 
// for IllegalArgumentException 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of Stack<Integer> 
            Stack<String> 
                stack = new Stack<String>(); 
  
            // Populating stack1 
            stack.add("A"); 
            stack.add("B"); 
            stack.add("C"); 
            stack.add("D"); 
            stack.add("E"); 
  
            // print stack 
            System.out.println("Original stack: "
                               + stack); 
  
            // getting the subList 
            // using subList() method 
            System.out.println("\nEndpoint indices "
                               + "are out of order"
                               + " (fromIndex > toIndex)"); 
            List<String> stack2 = stack.subList(7, 2); 
  
            // print the subList 
            System.out.println("SubStack of stack: "
                               + stack2); 
        } 
  
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
Original stack: [A, B, C, D, E]

Endpoint indices are out of order (fromIndex > toIndex)
java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)


相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Stack subList() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。