嗨,即使有一个可以返回的字符,我的代码也会抛出异常。这是我的代码
/** Return true if there is another character for the instance to return. */
public boolean hasNext() {
if(cursor<characterSource.length()) {
return true;
}
return false;
}
/** Returns the next character in the String. */
public Character next() throws NoSuchElementException {
if(hasNext()) {
int retVal=characterSource.indexOf(cursor);
return characterSource.charAt(retVal);
}
throw new NoSuchElementException();
}
我做错了什么?
最佳答案
假设 characterSource
是一个 String
或兼容的东西,我认为你应该删除这一行:
int retVal=characterSource.indexOf(cursor);
并调整另一个:
return characterSource.charAt(cursor++);
关于java - 使用异常的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52597845/