导致 Java 中出现 NoSuchElementException 的不同场景。
异常是程序执行过程中出现的问题(运行时错误)。发生异常时,程序会突然终止,并且生成异常的行之后的代码永远不会被执行。每个异常都由其各自的类表示。
NosuchElement 异常
这是一个运行时异常,即它发生在执行时。
在使用枚举、迭代器或标记器的访问器方法(例如 next() 或 nextElement())访问集合、数组或其他对象的内容时,如果您尝试从空对象中获取元素,或者如果您在到达对象(集合、数组或其他)的末尾后尝试获取下一个元素,则会生成 NoSuchElementException。
场景
枚举的 nextElement() 方法 − Vector、HashTable 等集合具有一个名为 elements() 的方法,该方法返回一个包含集合中所有元素的 Enumeration(接口)对象。
使用此对象,您可以使用 nextElement() 方法逐个获取元素。
如果您在空集合上调用此方法,或者在到达集合末尾后,将在运行时生成 NosuchElementException。
示例
import java.util.Enumeration;
import java.util.Vector;
public class EnumExample {
public static void main(String args[]) {
//实例化 Vector
Vector<Integer< vec = new Vector<Integer>( );
//填充向量
vec.add(1254);
vec.add(4587);
//使用枚举检索元素
Enumeration<Integer> en = vec.elements();
System.out.println(en.nextElement());
System.out.println(en.nextElement());
//到达末尾后检索下一个元素
System.out.println(en.nextElement());
}
}
Runtime error
1254 4587 Exception in thread "main" java.util.NoSuchElementException: Vector Enumeration at java.util.Vector$1.nextElement(Unknown Source) at MyPackage.EnumExample.main(EnumExample.java:18)
StringTokenizer 的 nextElement() 和 nextToken() 方法 − StringTokenizer 类接受字符串和分隔符作为其构造函数之一的参数,每次出现给定的分隔符时,将给定的字符串拆分为几个小标记。
此类的 nextToken() 和 nextElement() 方法返回标记器中的下一个标记。如果您在空的 Tokenizer 对象上调用这些方法,或者在到达末尾后,运行时将生成 NoSuchElementException。
示例
import java.util.StringTokenizer;
public class StringTokenizerExample{
public static void main(String args[]) {
String str = "Hello how are you";
//实例化 StringTokenizer 类
StringTokenizer tokenizer = new StringTokenizer(str, " ");
//打印所有 token
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
//到达末尾后获取下一个 token
tokenizer.nextToken();
tokenizer.nextElement();
}
}
Runtime error
Hello how are you Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at MyPackage.StringTokenizerExample.main(StringTokenizerExample.java:16)
Iterator 的 next() 方法 − Java 提供 Iterator 和 ListIterator 类来检索集合对象的元素。Iterator 和 ListIterator 的 next() 方法返回集合的下一个元素。
如果在空集合上调用这些方法,或者在到达末尾后,运行时会产生 NoSuchElementException。
类似地,ListIterator 的 previous() 方法返回集合的上一个元素,如果在空对象上调用此方法,或者在空对象的起始位置调用此方法,运行时会产生 NoSuchElementException。
示例
import java.util.ArrayList;
import java.util.Iterator;
public class NextElementExample{
public static void main(String args[]) {
//实例化 ArrayList 对象
ArrayList<String> list = new ArrayList<String>();
//填充 ArrayList
list.add("apples");
list.add("mangoes");
list.add("oranges");
//获取 ArrayList 的 Iterator 对象
Iterator it = list.iterator();
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
//到达末尾后检索下一个元素
it.next();
}
}
Runtime error
apples mangoes oranges Exception in thread "main" java.util.NoSuchElementException at java.util.ArrayList$Itr.next(Unknown Source) at MyPackage.NextElementExample.main(NextElementExample.java:19)
示例(previous() 方法)
import java.util.ArrayList;
import java.util.ListIterator;
public class NextElementExample{
public static void main(String args[]) {
//实例化 ArrayList 对象
ArrayList<String> list = new ArrayList<String>();
//填充 ArrayList
list.add("apples");
list.add("mangoes");
list.add("oranges");
//获取ArrayList的Iterator对象
ListIterator<String> it = list.listIterator();
it.next();
it.next();
it.next();
System.out.println(it.previous());
System.out.println(it.previous());
System.out.println(it.previous());
System.out.println(it.previous());
}
}
输出
oranges mangoes apples Exception in thread "main" java.util.NoSuchElementException at java.util.ArrayList$ListItr.previous(Unknown Source) at MyPackage.NextElementExample.main(NextElementExample.java:22)

