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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。