找不到符号 - 类迭代器 [Java]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26732819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:30:34  来源:igfitidea点击:

Can't find symbol - class Iterator [Java]

java

提问by Ryan Kenyon

I'm just starting out with Java and getting an error : "Cant find symbol - Class Iterator"

我刚开始使用 Java 并收到错误消息:“找不到符号 - 类迭代器”

What is wrong with my code?

我的代码有什么问题?

public class Notebook
{
    // Storage for an arbitrary number of notes.
    private ArrayList<String> notes;

    /**
     * Perform any initialization that is required for the
     * notebook.
     */
    public Notebook()
    {
        notes = new ArrayList<String>();
    }

    /**
     * Store a new note into the notebook.
     * @param note The note to be stored.
     */
    public void storeNote(String note)
    {
        notes.add(note);
    }

    /**
     * @return The number of notes currently in the notebook.
     */
    public int numberOfNotes()
    {
        return notes.size();
    }

    /**
     * Show a note.
     * @param noteNumber The number of the note to be shown.
     */
    public void showNote(int noteNumber)
    {
        if(noteNumber < 0) {
            // This is not a valid note number, so do nothing.
        }
        else if(noteNumber < numberOfNotes()) {
            // This is a valid note number, so we can print it.
            System.out.println(notes.get(noteNumber));
        }
        else {
            // This is not a valid note number, so do nothing.
        }
    }

    public void removeNote(int noteNumber)
    {
        if(noteNumber < 0){
            System.out.println("The index cannot be less than 0");
        }
        else if(noteNumber < numberOfNotes()){
            System.out.println("This is a valid note number so remove");
            notes.remove(noteNumber);
        }
        else {
            System.out.println("The index cannot be greater than the number of notes");
        }
    }

    public void listNotesForEach()
    {
        for(String note : notes){
           System.out.println(note);
        }
    }

    public void listNotesWhile()
    {
        int index = 0;
        while(index < notes.size()) {
            System.out.println(notes.get(index));
            index++;
        }
    }

    public boolean hasNote(String searchString)
    {
        int index = 0;
        boolean found = false;
        while(index < notes.size() && !found) {
            String note = notes.get(index);
            if(note.contains(searchString)) {
                //we don't need to keep looking
                found = true;
            }
            else {
                index++;
            }
        }
        //Either we found it, or we searched the whole collection return found;
        return found;
    }
        public void showNotes(String searchString)
    {
        int index = 0;
        boolean found = false;
        while(index < notes.size() && !found) {
            String note = notes.get(index);
            if(note.contains(searchString)) {
                System.out.println(index + " : " + note);
            }
            index++;
        }
    }
    public void listNotesIterator()
{
Iterator<String> it = notes.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}

}

回答by chrisb2244

Your error message tells you that it doesn't know about the class Iterator.

您的错误消息告诉您它不知道 class Iterator

Using an import statement will solve this problem.

使用导入语句将解决这个问题。

Add import java.util.Iterator;above your code.

import java.util.Iterator;在您的代码上方添加。

In general, whenever you use a class which is not within the built-in java.langpackage, you will need to import that class. Common examples come from java.util.ClassHere, like

通常,每当您使用不在内置java.lang包中的类时,您都需要导入该类。常见的例子来自java.util.ClassHere,比如

import java.util.List; // Class to hold a list of objects
import java.util.Scanner; // Class to read in keyboard etc entry
// my code here

回答by shalamus

Add the following code:

添加以下代码:

import java.util.Iterator;