首页 > Java > java教程 > 正文

Java NoSuchElementException

PHPz
发布: 2024-08-30 16:13:18
原创
948 人浏览过

Java中Enumeration中的nextElement方法、NamingEnumeration中的next方法等都会抛出NoSuchElementException异常。它表示枚举中没有其他元素了。该异常是RuntimeException异常的子类,并实现了Serialized接口。除了Enumeration之外,还有一些其他类会抛出此异常。以下是不同的类及其方法。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

  • StringTokenizer::nextElement()
  • 枚举::nextElement()
  • 迭代器::next()
  • NamingEnumeration::next()

NoSuchElementException 的语法、工作方式、构造函数和示例将在以下部分中解释。

声明:

以下是 NoSuchElementException 的声明。

public class NoSuchElementExceptionextends RuntimeException
登录后复制

NoSuchElementException 在 Java 中如何工作?

众所周知,异常是程序执行过程中发生的错误。程序被终止,并且当抛出异常时,导致异常的行之后的代码行将不会被执行。在不同的情况下会抛出NosuchElementException。他们是:

  • 在运行时对空枚举对象调用 Enumeration 类的 nextElement( ) 方法,或者当前位置为 Enumeration end 时,会抛出 NosuchElementException。
  • 在运行时在空枚举对象上调用 StringTokenizer 类的 nextElement( ) 或 nextToken() 方法或当前位置为 StringTokenizerend 时抛出 NosuchElementException。
  • 在运行时在空 Iterator 对象上调用 Iterator 类的 next( ) 方法或当前位置为 Iterator end 时抛出 NosuchElementException。
  • 在运行时,在空 ListIterator 对象上调用 ListIterator 类的 next( ) 方法时,或者当前位置为 ListIteratorend 时,会抛出 NosuchElementException。
  • 在运行时,在空 ListIterator 对象上调用 ListIterator 类的 previous ( ) 方法,或者当前位置为 ListIteratorstart 时,会抛出 NosuchElementException。

构造函数

以下是NoSuchElementException的两个构造函数

  • NoSuchElementException(): NoSuchElementException 将在不提供任何错误消息或通知作为字符串的情况下构造。
  • NoSuchElementException(Stringst): NoSuchElementException 将被构造,以字符串 st 的形式提供错误消息或通知。这用于稍后借助 getMessage 方法进行检索。包含错误的类名将出现在字符串 st.

Java NoSuchElementException 示例

让我们看看一些在 Java 中抛出 NoSuchElementException 的示例程序。

示例#1

Java 程序抛出 NoSuchElementException,因为 HashSet 中没有元素。

代码:

import java.util.HashSet;
import java.util.Set;
//class
public class NoExample {
//main method
public static void main(String[] args) {
//create an object for set s
Set s = new HashSet();
//select the next element
s.iterator().next();
}  }
登录后复制

输出:

Java NoSuchElementException

在此程序中,首先创建一个哈希集,然后使用 next() 方法选择集合中的下一个元素。由于集合中没有元素,因此抛出 NoSuchElementException。为了避免这种情况,可以在迭代集合之前进行检查,如下所示。

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
//class
public class NoExample {
//main method
public static void main(String[] args) {
Set e = new HashSet();
Iterator it = e.iterator();
//checks whether any element is present
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
登录后复制

为了检查元素是否存在,在现有程序中添加了 while 循环和迭代器。如果执行这段代码,可以看到没有抛出异常。

示例#2

由于 HashTable 中没有元素而抛出 NoSuchElementException 的 Java 程序

代码:

import java.util.Hashtable;
//class
public class NoExample {
//main method
public static void main(String[] args) {
//create an object for hashtable s
Hashtable s = new Hashtable();
//select the next element
s.elements().nextElement();
}  }
登录后复制

输出:

Java NoSuchElementException

在此程序中,首先创建一个哈希表,然后使用 nextElement() 方法选择表中的下一个元素。由于表中没有元素,因此抛出 NoSuchElementException。为了避免这种情况,可以在迭代表之前进行检查,如下所示。

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
//class
public class NoExample {
//main method
public static void main(String[] args) {
//create an object for hashtable s
Hashtable s = new Hashtable();
Set<String>k = s.keySet();
Iterator<String>i = k.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}  }
登录后复制

为了检查元素是否存在,在现有程序中添加了 while 循环、集合和迭代器。如果执行这段代码,可以看到没有抛出异常。

示例 #3

Java 程序抛出 NoSuchElementException,因为 StringTokenizer 和 Enumeration 中没有元素。

代码:

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
//class
public class NoExample {
private final static int el = 2;
//main method
public static void main(String[] args) {
//declare a string
String sn= "Happy Days never ends";
Hashtable s= new Hashtable(el);
Enumeration t = s.elements();
//create an object for StringTokenizer
StringTokenizer st = new StringTokenizer(sn, " ");
//Print the tokens
System.out.println(st.nextToken());
System.out.println(st.nextToken());
System.out.println(st.nextToken());
System.out.println(st.nextToken());
System.out.println(st.nextToken());
st.nextToken();
st.nextElement();
System.out.println(t.nextElement());
System.out.println(t.nextElement());
}
}
登录后复制

输出:

Java NoSuchElementException

In this program, a StringTokenizer is created first, and tokens are selected five times. As there are only four tokens, NoSuchElementException is thrown. In order to avoid this, a check can be given before iterating the Tokenizeras shown below.

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
//class
public class NoExample {
private final static int el = 2;
//main method
public static void main(String[] args) {
//declare a string
String sn= "Happy Days never ends";
Hashtable s= new Hashtable(el);
Enumeration t = s.elements();
//create an object for StringTokenizer
StringTokenizer st = new StringTokenizer(sn, " ");
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
登录后复制

In order to check the presence of elements, a while loop is added to the existing program. If this code is executed, it can be seen that no exceptions are thrown.

Java NoSuchElementException

Conclusion

NoSuchElementException is an exception that is thrown when there are no elements retrieved on calling the method next( ) and nextElement( ) in classes Iterator, StringTokenizer, Enumeration and NamingEnumeration. In this article, different aspects such as the declaration, working, constructors, and examples of NoSuchElementException is explained in detail.

以上是Java NoSuchElementException的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!