Java中导致NoSuchElementException的各种场景。
异常是在程序执行期间发生的错误(运行时错误)。当发生异常时,程序会突然终止,异常行之后的代码将不会执行。每个异常都由其相应的类表示。
NoSuchElementException
这是一个运行时异常,即它在运行时发生。
使用枚举、迭代器或标记器的访问器方法(例如 next() 或 nextElement())访问集合、数组或其他对象的內容时,如果尝试从空对象获取元素,或者尝试在到达对象(集合、数组或其他)末尾后获取下一个元素,则会生成 NoSuchElementException。
场景
Enumeration 的 nextElement() 方法 − 像 Vector、HashTable 等集合具有名为 elements() 的方法,该方法返回一个包含集合中所有元素的 Enumeration(接口)对象。
使用此对象,您可以使用 nextElement() 方法逐个获取元素。
如果在空集合上调用此方法,或者在到达集合末尾后调用此方法,则会在运行时生成 NoSuchElementException。
示例
import java.util.Enumeration;
import java.util.Vector;
public class EnumExample {
public static void main(String args[]) {
//instantiating a Vector
Vector<Integer< vec = new Vector<Integer>( );
//Populating the vector
vec.add(1254);
vec.add(4587);
//Retrieving the elements using the Enumeration
Enumeration<Integer> en = vec.elements();
System.out.println(en.nextElement());
System.out.println(en.nextElement());
//Retrieving the next element after reaching the end
System.out.println(en.nextElement());
}
}运行时错误
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() 方法返回标记器中的下一个标记。如果在空标记器对象上调用这些方法,或者在到达末尾后调用这些方法,则会在运行时生成 NoSuchElementException。
示例
import java.util.StringTokenizer;
public class StringTokenizerExample{
public static void main(String args[]) {
String str = "Hello how are you";
//Instantiating the StringTokenizer class
StringTokenizer tokenizer = new StringTokenizer(str, " ");
//Printing all the tokens
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
//Getting the next token after reaching the end
tokenizer.nextToken();
tokenizer.nextElement();
}
}运行时错误
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[]) {
//Instantiating an ArrayList object
ArrayList<String> list = new ArrayList<String>();
//populating the ArrayList
list.add("apples");
list.add("mangoes");
list.add("oranges");
//Getting the Iterator object of the ArrayList
Iterator it = list.iterator();
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
//Retrieving next element after reaching the end
it.next();
}
}运行时错误
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[]) {
//Instantiating an ArrayList object
ArrayList<String> list = new ArrayList<String>();
//populating the ArrayList
list.add("apples");
list.add("mangoes");
list.add("oranges");
//Getting the Iterator object of the ArrayList
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)
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP