📜  Java中的堆栈listIterator(int)方法与示例

📅  最后修改于: 2022-05-13 01:54:24.807000             🧑  作者: Mango

Java中的堆栈listIterator(int)方法与示例

Stack 类listIterator(int)方法用于返回列表中元素的列表迭代器(以正确的顺序),从列表中的指定位置开始。指定的索引指示初始调用 next 将返回的第一个元素。对 previous 的初始调用将返回具有指定索引减一的元素。返回的列表迭代器是快速失败的。

句法:

public ListIterator listIterator(int index)

参数:此方法将第一个元素的索引作为要从列表迭代器返回的参数(通过调用 next)

返回值:此方法从列表中的指定位置开始返回此列表中元素的列表迭代器(按正确顺序)。

异常:如果索引超出范围(索引大小()),此方法将引发IndexOutOfBoundsException

下面是说明listIterator()方法的示例。

示例 1:

// Java program to demonstrate
// listIterator() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of Stack
            Stack stack = new Stack();
  
            // adding element to stack
            stack.add("A");
            stack.add("B");
            stack.add("C");
            stack.add("D");
  
            // print stack
            System.out.println("Stack: "
                               + stack);
  
            // getting iterated value starting from index 2
            // using listIterator() method
            ListIterator
                iterator = stack.listIterator(2);
  
            // Printing the iterated value
            System.out.println("\nUsing ListIterator"
                               + " from Index 2:\n");
            while (iterator.hasNext()) {
                System.out.println("Value is : "
                                   + iterator.next());
            }
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Stack: [A, B, C, D]

Using ListIterator from Index 2:

Value is : C
Value is : D

示例 2:对于IndexOutOfBoundsException

// Java program to demonstrate
// listIterator() method
// for IndexOutOfBoundsException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of Stack
            Stack
                stack = new Stack();
  
            // adding element to stack
            stack.add("A");
            stack.add("B");
            stack.add("C");
            stack.add("D");
  
            // print stack
            System.out.println("Stack: "
                               + stack);
            System.out.println("Size of Stack: "
                               + stack.size());
  
            // Get ListIterator from index 7
            // using listIterator() method
            System.out.println("Trying to getListIterator"
                               + " from index 7\n");
  
            ListIterator
                iterator = stack.listIterator(7);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Stack: [A, B, C, D]
Size of Stack: 4
Trying to getListIterator from index 7

Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7