摘要顺序表 listIterator(): Java中的方法用于获取列表迭代器。它返回列表中元素的列表迭代器(按正确的顺序)。
null
语法:
public abstract ListIterator listIterator(int index)
参数: 此方法接受一个参数 指数 这是列表迭代器(通过调用下一个方法)返回的第一个元素的索引
返回: 此方法返回列表中元素的列表迭代器(按正确顺序)。
例外情况: 这个方法抛出 IndexOutOfBoundsException ,如果索引超出范围(索引大小())
下面是演示ListIterator()的代码:
项目1:
// Java program to demonstrate // add() method import java.util.*; public class GfG { public static void main(String[] args) { // Creating an instance of the AbstractSequentialList AbstractSequentialList<Integer> absl = new LinkedList<>(); // adding elements to absl absl.add( 5 ); absl.add( 6 ); absl.add( 7 ); absl.add( 2 , 8 ); absl.add( 2 , 7 ); absl.add( 1 , 9 ); absl.add( 4 , 10 ); // Getting ListIterator ListIterator<Integer> Itr = absl.listIterator( 2 ); // Traversing elements while (Itr.hasNext()) { System.out.print(Itr.next() + " " ); } } } |
输出:
6 7 10 8 7
项目2: 演示IndexOutOfBoundException
// Java code to show IndexOutofBoundException import java.util.*; public class GfG { public static void main(String[] args) { // Creating an instance of the AbstractSequentialList AbstractSequentialList<Integer> absl = new LinkedList<>(); // adding elements to absl absl.add( 5 ); absl.add( 6 ); absl.add( 7 ); absl.add( 2 , 8 ); absl.add( 2 , 7 ); absl.add( 1 , 9 ); absl.add( 4 , 10 ); // Printing the list System.out.println(absl); try { // showing IndexOutOfBoundsException // Getting ListIterator ListIterator<Integer> Itr = absl.listIterator( 15 ); // Traversing elements while (Itr.hasNext()) { System.out.print(Itr.next() + " " ); } } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
输出:
[5, 9, 6, 7, 10, 8, 7] Exception: java.lang.IndexOutOfBoundsException: Index: 15, Size: 7
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END