异常处理系列:java.util.NoSuchElementException
2014-10-09 12:05
441 查看
出现这个异常的话,那就是你的线程越界了,这个时候你就要检查下到底是哪里越界了。
这里有我出错的代码:
如果我们按照如上做了就会报异常:
因为我们虽然知道ls集合只有三条数据,但是编译器不知道。
解决异常:
这里仅仅是列出了Iterator迭代器,其他的集合只要能遍历的都可能会出现异常
这里有我出错的代码:
List<String> ls = new ArrayList<String>(); ls.add("张三"); ls.add("李四"); ls.add("王五"); //接下来我们想要用Iterator迭代器对ls集合遍历 Iterator it = ls.iterator(); for(int i =0;i <3;i++){ System.out.println(it.next()); }
如果我们按照如上做了就会报异常:
因为我们虽然知道ls集合只有三条数据,但是编译器不知道。
解决异常:
List<String> ls = new ArrayList<String>(); ls.add("张三"); ls.add("李四"); ls.add("王五"); //接下来我们想要用Iterator迭代器对ls集合遍历 Iterator it = ls.iterator(); while(it.hasNext()){ System.out.println(it.next()); }
这里仅仅是列出了Iterator迭代器,其他的集合只要能遍历的都可能会出现异常
相关文章推荐
- 【JAVA学习】Iterator迭代器导致的java.util.NoSuchElementException原因与解决方法
- java.util.NoSuchElementException at java.util.AbstractList$Itr.next(AbstractList.java:350) at
- java.util.NoSuchElementException解决办法
- java中Exception in thread "main" java.util.NoSuchElementException
- 迭代器(Iterator)的java.util.NoSuchElementException错误的改正
- java.util.NoSuchElementException: No mapping for class sun.awt.AppContext
- java.util.NoSuchElementException: Timeout waiting for idle object
- Sqoop导入数据到MySql时的java.util.NoSuchElementException异常
- java.util.NoSuchElementException原因
- org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted和java.util.NoSuchElementException: Timeout wa
- java.util.NoSuchElementException错误 解决办法
- java.util.NoSuchElementException解决办法
- ibatis报错:Caused by: java.util.NoSuchElementException
- java.util.NoSuchElementException: Hashtable Enumerator
- java.util.NoSuchElementException解决办法
- 关于java.util.NoSuchElementException错误
- java.util.NoSuchElementException解决办法
- 错误 java.util.NoSuchElementException:Hashtable Enumerator
- Sqoop导入数据到MySql时的java.util.NoSuchElementException异常2
- java.util.NoSuchElementException: Timeout waiting for idle object