遍历取值异常(Hashtable Enumerator)

    用迭代器取值时抛出的异常:java.util.NoSuchElementException: Hashtable Enumerator
    代码示例:
//使用迭代器遍历
			Iterator<String> it = tableProper.stringPropertyNames().iterator();			
			sqlMap = new HashMap<String,String>();
			while(it.hasNext()){
				sqlMap.put(it.next(), tableProper.getProperty(it.next()));
			}

    这是一个枚举异常,是因为在还没来得及执行it.next()时就开始引用它。我们可以用如下方式解决此问题:
//使用迭代器遍历
			Iterator<String> it = tableProper.stringPropertyNames().iterator();			
			sqlMap = new HashMap<String,String>();
			String key;
			while(it.hasNext()){
				key = it.next();
				sqlMap.put(key, tableProper.getProperty(key));
			}

    原文永久地址: http://jsonliangyoujun.iteye.com/blog/2360983

猜你喜欢

转载自jsonliangyoujun.iteye.com/blog/2360983