use*_*051 2 java string object mismatch
在我的代码中创建搜索方法以搜索字符串时,我一直收到此错误.我已经经历了很多试图解决这个问题的例子,但我找不到.感谢您的帮助,并建议您给予.
public class runNote {
public static void main(String[] args) {
// TODO Auto-generated method stub
Notebook note = new Notebook();
note.storeNote("happy");
note.storeNote("hello there");
note.storeNote("work at 5");
note.storeNote("BBQ Time");
note.storeNote("UNI!!!!");
note.storeNote("Dont miss lecture at 9:15");
System.out.println(note.numberOfNotes());
note.showNote(1);
note.searchNotes("hap");
}}
public class Notebook{
/**
* Perform any initialization that is required for the
* notebook.
*/
public Notebook()
{
notes = new ArrayList();
}
/**
* 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();
}
/**
* A simple search engine to find the correct notes.
*/
public void searchNotes(String search){
for (String item : notes){
if (item.contains(search)){
System.out.println(item);
}
}
}
/**
* 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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
你在这一行有例外:
for (String item : notes)
Run Code Online (Sandbox Code Playgroud)
notes是一个ArrayList声明为原始类型,虽然我没有看到声明我可以看到这行初始化:
notes = new ArrayList();
Run Code Online (Sandbox Code Playgroud)
它认为它的元素类型的Object你需要声明notes 与ArrayList<String>类型,如:
ArrayList<String> notes = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25499 次 |
| 最近记录: |