Exception in thread "main" java.util.NoSuchElementException at java.util.ArrayList$Itr.next(Unknown

版权声明:为中华之崛起而读书。转载请注明出处: https://blog.csdn.net/Kj_Gym/article/details/82926017

源码:

public static void printEmployeeInfo(List<Map<String, Object>> list) {
		for(Iterator<Map<String, Object>> it = list.iterator(); it.hasNext();) {
			System.out.print(it.next().get("ID")+"\t");
			System.out.print(it.next().get("姓名")+"\t");
			System.out.print(it.next().get("薪水")+"\t");
			System.out.print(it.next().get("单位")+"\t");
			System.out.println(it.next().get("入职时间"));
		}
	}

报错的原因是:每调用一次it.next(),则记录的“指针”便会指向下一条。所以需要定义一个局部变量来接收一下。
解决办法:

public static void printEmployeeInfo(List<Map<String, Object>> list) {
		for(Iterator<Map<String, Object>> it = list.iterator(); it.hasNext();) {
			Map<String, Object> map = it.next();
			System.out.print(map.get("ID")+"\t");
			System.out.print(map.get("姓名")+"\t");
			System.out.print(map.get("薪水")+"\t");
			System.out.print(map.get("单位")+"\t");
			System.out.println(map.get("入职时间"));
		}
	}

猜你喜欢

转载自blog.csdn.net/Kj_Gym/article/details/82926017
今日推荐