Java Collections emptyListIterator()
Java集合类的
emptyListIterator()方法用于获取没有元素的列表迭代器。
语法
以下是
emptyListIterator()方法的声明:
public static <T> ListIterator<T> emptyListIterator()
参数
此方法不接受任何参数。
返回
emptyListIterator()方法返回没有元素的列表迭代器。.
异常
NoSuchElementException
兼容性版本
Java 1.7及更高版本
示例1
import java.util.*;
public class CollectionsEmptyListIteratorExample1 {
public static void main(String[] args) {
ListIterator<String> Itr = Collections.emptyListIterator();
System.out.println("Output: "+Itr.hasNext());
}
}
输出:
示例2
import java.util.*;
public class CollectionsEmptyListIteratorExample2 {
public static void main(String[] args) {
List<Integer> ls = new ArrayList<Integer>();
ls.add(1);
ls.add(2);
ListIterator<String> Itr = Collections.emptyListIterator();
System.out.println(Itr.next());
}
}
输出:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Collections$EmptyIterator.next(Collections.java:4190)
at myPackage.CollectionsEmptyListIteratorExample2.main(CollectionsEmptyListIteratorExample2.java:10)
示例3
import java.util.*;
public class CollectionsEmptyListIteratorExample3 {
public static void main(String[] args) {
List<Integer> ls = new ArrayList<Integer>();
ls.add(1);
ls.add(2);
ListIterator<String> Itr = Collections.emptyListIterator();
System.out.println(Itr.nextIndex());
System.out.println(Itr.previousIndex());
System.out.println("nextIndex() and previousIndex() always returns 0 and -1.");
}
}
输出:
0
-1
nextIndex() and previousIndex() always returns 0 and -1.