Java Stack subList()方法与实例
Java.util.Stack 类的 subList() 方法用于返回该Stack中指定的fromIndex(包括)和toIndex(不包括)之间的部分的视图。(如果fromIndex和toIndex相等,返回的Stack是空的)。
返回的堆栈由这个堆栈支持,所以返回的堆栈中的非结构性变化会反映在这个堆栈中,反之亦然。返回的堆栈支持所有可选的堆栈操作。
语法
public Stack subList(int fromIndex, int toIndex)
Java
参数: 该方法需要以下参数作为参数。
- fromIndex – 子列表的低端点(包括)。
- toIndex – 子列表的高端点(不包括)。
返回值: 该方法返回该堆栈中指定范围的视图。
异常: 该方法会抛出以下异常。
- 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);
}
}
}
Java
输出:
Original stack: [A, B, C, D, E]
SubStack of stack: [C, D]
Java
例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);
}
}
}
Java
输出:
Original stack: [A, B, C, D, E]
End index value is out of range
java.lang.IndexOutOfBoundsException: toIndex = 7
Java
例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);
}
}
}
Java
输出:
Original stack: [A, B, C, D, E]
Endpoint indices are out of order (fromIndex > toIndex)
java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)
Java