NoSuchElementException
生活随笔
收集整理的這篇文章主要介紹了
NoSuchElementException
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在之前下項(xiàng)目的時(shí)候遇到這個(gè).NoSuchElementException異常,當(dāng)時(shí)我寫到一個(gè)大類的結(jié)尾。但是編譯器。從未報(bào)錯(cuò)。然而在運(yùn)行的時(shí)候出現(xiàn)了這樣的異常,非常頭疼 ,一到運(yùn)行時(shí)候就報(bào)異常,我就上網(wǎng)搜索了一下,才明白,這是我的剛開始寫的代碼,下面寫出這個(gè)錯(cuò)誤的例子及分析,希望能及時(shí)的幫助和我同樣犯了小錯(cuò)誤的菜鳥!
package com.PETProgram;import java.util.Scanner;
import java.util.Iterator;
import java.util.ArrayList;
import static java.lang.System.out;
public class PetSystemLogin {
?public static Scanner input = new Scanner(System.in);
??????
?????? Pet p1 = new Pet("花花");
?public static void main(String[] args) {
????? out.println("寵物商店正在啟動(dòng)");
????? out.println("Wonder land醒來,所有寵物從oracle中醒來");
????? out.println("*********************************");
????? out.println("寵物商店正在啟動(dòng)");
???? ArrayList <Pet>list = new ArrayList<Pet>();
????? list.add(new Pet("花花"));
????? list.add(new Pet("貝貝"));
????? list.add(new Pet("成成"));
????? list.add(new Pet("露露"));
????? list.add(new Pet("老虎"));
????? list.add(new Pet("老虎"));
????? list.add(new Pet("1"));
????? list.add(new Pet("shizi"));
????? Iterator<Pet> it = list.iterator();
????? while(it.hasNext())
????? {?? int i = 0;
??? ?
????? for(;i<list.size();i++)
??? ?? {
??? ?? System.out.println("第"+(i+1)+"個(gè)"+"寵物,"+"名字叫"+((Pet)it.next()).name);
??? ??
??? ?? }
????? }
???
?
????? out.println("********************************");
????? out.println("所有寵物主人從oracle中醒來");
????? out.println("********************************");
????? ArrayList<PetOwner> list1 = new ArrayList<PetOwner>();
??????? PetOwner pet = new PetOwner("小明");
??????? list1.add(pet);
??????? list1.add(new PetOwner("小強(qiáng)"));
??????? Iterator<PetOwner> it1 = list1.iterator();
??????? while(it1.hasNext())
??????? {?? int i = 0;
????? ?? for(;i<list1.size();i++)
????? ?? {
????? ?? System.out.println("第"+(i+1)+"個(gè)"+"寵物主人,"+"名字叫"+((PetOwner)it1.next()).name);
????? ?? }
?????? }
??????? out.print("所有寵物商店從oracle中醒來");
??????? out.print("************************************\n");
??????? ArrayList<PetStore> list2 = new ArrayList<PetStore>();
??????? PetStore pets = new PetStore("北京西苑");
??????? list2.add(pets);
??????? list2.add(new PetStore("重慶觀音橋"));
??????? Iterator<PetStore> it2 = list2.iterator();
??????? while(it2.hasNext())//錯(cuò)誤在這里,原來的迭代的元素還是寫在還在迭代上面的元素,其實(shí)應(yīng)該迭代現(xiàn)在是it2
??????? {
??????? ? out.println("我的名字叫:"+(it2.next()).Storename);
??????? }
??????
??????? out.println("請(qǐng)選擇登陸模式,輸入1為寵物主人登陸,輸入2為寵物商店登陸");
??????? int choice = input.nextInt();
??????? if(choice ==1)
??????? {
??????? ?pet.OwnerLogin();
??????? ?pet.isAccount();
??????? }else if(choice ==2)
??????? {
??????? ?pets.StoreLogin();
??????? }else if(choice==0)
??????? {
??????? ?System.exit(0);
??????? }
???????
?}
???
?
}
?
?元素迭代錯(cuò)誤的錯(cuò)誤造成的,其實(shí)
? 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的NoSuchElementException的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用log.debug()
- 下一篇: mysql中like % %模糊查询