首頁 > 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學習者快速成長!