Java NoSuchElementException
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(); } }
輸出:
在此程式中,先建立一個雜湊集,然後使用 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(); } }
輸出:
在此程式中,先建立一個雜湊表,然後使用 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()); } }
輸出:
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.
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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

AI可以幫助優化Composer的使用,具體方法包括:1.依賴管理優化:AI分析依賴關係,建議最佳版本組合,減少衝突。 2.自動化代碼生成:AI生成符合最佳實踐的composer.json文件。 3.代碼質量提升:AI檢測潛在問題,提供優化建議,提高代碼質量。這些方法通過機器學習和自然語言處理技術實現,幫助開發者提高效率和代碼質量。

MySQL函數可用於數據處理和計算。 1.基本用法包括字符串處理、日期計算和數學運算。 2.高級用法涉及結合多個函數實現複雜操作。 3.性能優化需避免在WHERE子句中使用函數,並使用GROUPBY和臨時表。

HTML5帶來了五個關鍵改進:1.語義化標籤提升了代碼清晰度和SEO效果;2.多媒體支持簡化了視頻和音頻嵌入;3.表單增強簡化了驗證;4.離線與本地存儲提高了用戶體驗;5.畫布與圖形功能增強了網頁的可視化效果。

typetraits在C 中用於編譯時類型檢查和操作,提升代碼的靈活性和類型安全性。 1)通過std::is_integral和std::is_floating_point等進行類型判斷,實現高效的類型檢查和輸出。 2)使用std::is_trivially_copyable優化vector拷貝,根據類型選擇不同的拷貝策略。 3)注意編譯時決策、類型安全、性能優化和代碼複雜性,合理使用typetraits可以大大提升代碼質量。

在MySQL中配置字符集和排序規則的方法包括:1.設置服務器級別的字符集和排序規則:SETNAMES'utf8';SETCHARACTERSETutf8;SETCOLLATION_CONNECTION='utf8_general_ci';2.創建使用特定字符集和排序規則的數據庫:CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci;3.創建表時指定字符集和排序規則:CREATETABLEexample_table(idINT

MySQL中重命名數據庫需要通過間接方法實現。步驟如下:1.創建新數據庫;2.使用mysqldump導出舊數據庫;3.將數據導入新數據庫;4.刪除舊數據庫。

在C 中實現單例模式可以通過靜態成員變量和靜態成員函數來確保類只有一個實例。具體步驟包括:1.使用私有構造函數和刪除拷貝構造函數及賦值操作符,防止外部直接實例化。 2.通過靜態方法getInstance提供全局訪問點,確保只創建一個實例。 3.為了線程安全,可以使用雙重檢查鎖定模式。 4.使用智能指針如std::shared_ptr來避免內存洩漏。 5.對於高性能需求,可以使用靜態局部變量實現。需要注意的是,單例模式可能導致全局狀態的濫用,建議謹慎使用並考慮替代方案。

Java代碼可以在不同操作系統上無需修改即可運行,這是因為Java的“一次編寫,到處運行”哲學,由Java虛擬機(JVM)實現。 JVM作為編譯後的Java字節碼與操作系統之間的中介,將字節碼翻譯成特定機器指令,確保程序在任何安裝了JVM的平台上都能獨立運行。
