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)