Java 类型不匹配无法从元素类型对象转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20445428/
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
type mismatch cannot convert from element type object to string
提问by user202051
I keep getting this error when making a search method in my code to search through string. I have gone through alot of examples trying to fix this but I cant find any. Thank you for any help and advise you can give.
在我的代码中使用搜索方法来搜索字符串时,我不断收到此错误。我经历了很多试图解决这个问题的例子,但我找不到任何例子。感谢您提供的任何帮助和建议。
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.
}
}
}
采纳答案by openmike
Add notes as a global variable to your class
将笔记作为全局变量添加到您的班级
public class Notebook{
ArrayList<String> notes = null;
....
}
In the constructor, do:
在构造函数中,执行:
public Notebook()
{
notes = new ArrayList<String>();
}
回答by Sage
You are having exception at this line :
您在这一行遇到异常:
for (String item : notes)
notes
is an ArrayList
declared as raw type, although i don't see the declaration i can see this line of initialization:
notes
是一个ArrayList
声明为原始类型,虽然我没有看到声明我可以看到这行初始化:
notes = new ArrayList();
which sees it's element as of type Object
You need to declare notes
with ArrayList<String>
type such as:
它将它的元素视为类型Object
您需要notes
使用ArrayList<String>
类型声明,例如:
ArrayList<String> notes = new ArrayList<>();
回答by Palejandro
try this: notes = new ArrayList<String>();
尝试这个: notes = new ArrayList<String>();
回答by hrv
You don't declare the notes variable
你没有声明 notes 变量
public class Notebook{
private ArrayList<String> notes;
...