Java.util.Collections 类

java.util.Collections.emptyListIterator() 方法返回一个没有元素的列表迭代器。更准确地说:

  • hasNext 始终返回 false
  • nextprevious 总是抛出 NoSuchElementException
  • removeset 总是抛出 IllegalStateException>.
  • add 总是抛出 UnsupportedOperationException
  • nextIndex 总是返回 0.
  • previousIndex 始终返回 -1

语法

public static <T> ListIterator<T> emptyListIterator()

这里,T是迭代器中元素的类型(如果有的话)。

参数

不需要参数。

返回值

返回一个空列表迭代器。

异常

无。

示例:

在下面的示例中,java.util.Collections.emptyListIterator() 方法用于创建一个空列表迭代器。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建一个空的迭代器
    ListIterator<Integer> Itr = Collections.emptyListIterator();

    //打印迭代器的内容
    while(Itr.hasNext()) {
      System.out.println(Itr.next());
    }
    System.out.println("List Iterator is empty.");
  }
}

上面的代码将是:

List Iterator is empty.

示例:

让我们再看一个示例来详细了解该方法。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建一个空的迭代器
    ListIterator<Integer> Itr = Collections.emptyListIterator();

    //打印nextIndex和previousIndex
    System.out.println(Itr.nextIndex());
    System.out.println(Itr.previousIndex());
  }
}

输出上面的代码将是:

0
-1