Show position of item in a list

后端 未结 1 990
青春惊慌失措
青春惊慌失措 2020-12-22 08:24

I am working with a link list. My constructor takes arrays with items in it or appends an item(one at time) to the list through BtnAddTree click. For experiment

相关标签:
1条回答
  • 2020-12-22 09:15

    Develop a way to compare the object properly. is tree type unique? maybe you need a full icomparable?

    then create a method in the listoftrees class

    public int getPosition(fruit_trees tree)
    {
     fruit_trees ft = first_tree;
     if(ft.tree_type == tree.tree_type)
     {
          return 0; //or return 1 depending on how you want to index stuff
     }
     int i = 1;//possibly set to 2 if returning 1 above
     while(ft.next_tree != null)
     {
         ft = ft.next_tree;
         if(ft.tree_type == tree.tree_type)
         {
            return i; 
         }
         i++;
     }
    
     return -1; //or something else here to show that its not in the list
    
    }
    

    also as a side note ..Dont set the label value in the class in the method Current_Tree(). That should return a fruit_trees object and the btn click methods should set the label value .

    you should actually just get rid of that method completely(unless there is some reason for it outside of the code above.

    0 讨论(0)
提交回复
热议问题