今天在做编译原理实验的时候,我用到java.util.HashMap
Set ba = basic.keySet();
Set nu = num.keySet();
Set sy = symbalchar.keySet();
Set ot = other.keySet();
Iterator ba1 =ba.iterator();
Iterator nu1 =nu.iterator();
Iterator sy1 =sy.iterator();
Iterator oth1 =ot.iterator();
while(ba1.hasNext())
{
String bnex =(String)ba1.next();
String bre =(String)basic.get(bnex);
result.append("("+bnex+","+bre+")"+"\r\n");
}
while(nu1.hasNext())
{
String nnex =(String)nu1.next();
String nre =(String)num.get(nnex);
result.append("常数:"+"("+nnex+","+nre+")"+"\r\n");
}
while(sy1.hasNext())
{
String snex =(String)sy1.next();
int sre =(Integer)symbalchar.get(snex);
result.append("标识符:"+"("+snex+","+sre+")"+"\r\n");
}
while(oth1.hasNext())
{
String onex =(String)oth1.next();
String ore =(String)other.get(onex);
result.append("("+onex+","+ore+")"+"\r\n");
}
时,遇到了java.util.NoSuchElementException.
经过查找发现是由于Iterator 发生越界,导致异常,
当时的错误代码是
<pre name="code" class="java">result.append("("+ba1.next()+","+basichar.get(ba1.next())+")"+"\r\n");
//basichar是HashMap的一个对象
原来出现两个ba1.next()从而导致越界异常,后改为最上方的代码,问题成功解决