type mismatch cannot convert from element type object to string

后端 未结 4 452
日久生厌
日久生厌 2021-01-14 22:15

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

相关标签:
4条回答
  • 2021-01-14 22:30

    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 = new ArrayList();
    

    which sees it's element as of type Object You need to declare notes with ArrayList<String> type such as:

      ArrayList<String> notes = new ArrayList<>();
    
    0 讨论(0)
  • 2021-01-14 22:35

    You don't declare the notes variable

    public class Notebook{
      private ArrayList<String> notes;
    ...
    
    0 讨论(0)
  • 2021-01-14 22:42

    try this: notes = new ArrayList<String>();

    0 讨论(0)
  • 2021-01-14 22:47

    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>();
    }
    
    0 讨论(0)
提交回复
热议问题