java - 使用异常的迭代器

标签 java exception

嗨,即使有一个可以返回的字符,我的代码也会抛出异常。这是我的代码

/** 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/

相关文章:

java - 试图连接到 tomcat6 : java.net.UnknownHostException 上的 mysql 服务器

java - 第二次无法运行maven嵌入式tomcat

java - 试图导致 java.lang.OutOfMemoryException

java - 修剪 Java 字符串中不需要的字符

java - 引用类型是通过引用复制的吗?

java - Java 中的 For 循环

java - 永远不会在相应的 try 语句的主体中抛出异常

Android Log.X 不打印堆栈跟踪

c++ - 从构造函数的初始化列表中捕获异常

java.lang.NumberFormatException : For input string: "100" while executing MapReduce 异常