NoSuchElementException 的原因是什么?如何在 Java 中修复它?
NoSuchElementException 的原因是什么?如何在 Java 中修复它?
异常是在程序执行期间发生的错误(运行时错误)。当发生异常时,程序会突然终止,异常行之后的代码将不会执行。每个异常都由其相应的类表示。
NoSuchElementException 的原因
这是一个运行时异常,即它在执行时发生。
使用枚举、迭代器或标记器的访问器方法(例如 next() 或 nextElement())访问集合、数组或其他对象的内容时,如果尝试从空对象获取元素,或者尝试在到达对象(集合、数组或其他)的末尾后获取下一个元素,则会生成 NoSuchElementException。
例如:
- 如果在空枚举对象上调用 Enumeration 类的 nextElement() 方法,或者当前位置位于枚举的末尾,则会在运行时生成 NoSuchElementException。
- 如果在空 StringTokenizer 对象上调用 StringTokenizer 类的 nextElement() 和 nextToken() 方法,或者当前位置位于 StringTokenizer 的末尾,则会在运行时生成 NoSuchElementException。
- 如果在空迭代器/列表迭代器上调用 Iterator 或 ListIterator 类的 next() 方法,或者当前位置位于末尾,则会在运行时生成 Iterator/ListIterator NoSuchElementException。
- 同样,如果在空 ListIterator 对象上调用 ListIterator 类的 previous() 方法,或者当前位置位于 ListIterator 的开头,则会在运行时生成 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)
处理/修复 NoSuchElementException
几乎所有其访问器方法会导致 NoSuchElementException 的类都包含各自的方法来验证对象(集合、标记器等)是否包含更多元素。
例如:
- Enumeration 类包含一个名为 hasMoreElements() 的方法,如果当前对象在当前位置之后包含更多元素,则返回 true(否则返回 false)。
- StringTokenizer 类包含名为 hasMoreTokens() 和 hasMoreElements() 的方法,如果当前对象在当前位置之后包含更多元素,则返回 true(否则返回 false)。
- Iterator 类包含 hasNext() 方法,如果当前迭代器在当前位置之后包含更多元素,则返回 true(否则返回 false)。
- ListIterator 类包含 hasPrevious() 方法,如果当前迭代器在当前位置之前包含更多元素,则返回 true(否则返回 false)。
在 while 循环中,使用这些方法验证相应对象是否包含更多元素,仅当条件为 true 时才打印/访问元素。这可以防止在对象中没有元素或到达末尾时使用访问器方法访问元素。
Enumeration 类的 hasMoreElements() 方法
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();
while(en.hasMoreElements()) {
System.out.println(en.nextElement());
}
}
}输出
1254 4587
StringTokenizer 类的 nextMoreTokens() 方法 −
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
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}输出
Hello how are you
Iterator 类的 hasNext() 方法 −
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();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}输出
apples mangoes oranges
ListIterator 类的 hasPrevious() 方法 −
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();
while(it.hasNext()) {
it.next();
}
while(it.hasPrevious()) {
System.out.println(it.previous());
}
}
}输出
oranges mangoes apples
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP