I'm just starting out with Java and getting an error : "Cant find symbol - Class Iterator"
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());
}
}
}
Your error message tells you that it doesn't know about the class Iterator
.
Using an import statement will solve this problem.
Add import java.util.Iterator;
above your code.
In general, whenever you use a class which is not within the built-in java.lang
package, you will need to import that class. Common examples come from java.util.ClassHere
, like
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
添加以下代码:
import java.util.Iterator;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.