0

我正在使用链接列表。BtnAddTree我的构造函数采用其中包含项目的数组或通过单击将一个项目(一次一个)附加到列表中。出于实验原因,我试图找出一种从列表中检索项目位置的方法。我已经设置了三个希望显示的按钮:first, next, lastitem 和 display through a label。我创建了两个函数来满足我的需要RetrieveCurrent_Tree()但在这里我走到了死胡同。我知道要显示next我可能需要使用循环但不确定如何进行的项目。如何显示在列表中找到的项目的位置?

        public class ListForTrees
        {

        //Retrieve Position of item
            public void Retrieve(int Position)
            {
                int new_position = Position;
                fruit_trees current = first_tree;

                for (int i = 0; i < Position && current != null; i++)
                {
                    current = current.next_tree;
                }
                return current;
            }

        //Show Current Tree
            public void Current_Tree()
            {
                try
                {
                    current = fruit_trees.first_tree;
                    labelSpecificTree.Text = current.Type.ToString();
                }
                catch { };

            }         



        }

        ListForTrees mainlist = new ListForTrees();     

        private void BtnGo_Click(object sender, EventArgs e)
        {
            fruit_trees[] ax = {   new fruit_trees("cherry", 48, 12.95, 3),
                                             new fruit_trees("pine", 36, 9.95, 8),
                                             new fruit_trees("oak", 60, 14.95, 2),
                                             new fruit_trees("peach", 54, 19.95, 3),
                                             new fruit_trees("pear", 36, 11.85, 2),
                                             new fruit_trees("apple", 62, 13.45, 5)
                                         };   
            mainlist = new ListForTrees(ax);
            fruit_trees current = mainlist.first_tree;   

            while (current != null)
            {
                TxtOutput.AppendText(current.ToString() + Environment.NewLine);
                current = current.next_tree;
            }
        }

        private void ShowNextItem_Click(object sender, EventArgs e)
        {
        //Show Next Item
        }

        private void ShowLastItem_Click(object sender, EventArgs e)
        {
        //Show Last Item
        }

        private void ShowFirstItem_Click_1(object sender, EventArgs e)
        {
        // Show First Item
        }


    }
}
4

1 回答 1

2

开发一种正确比较对象的方法。树类型是唯一的吗?也许你需要一个完全无与伦比的?

然后在 listoftrees 类中创建一个方法

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

}

也作为旁注..不要在方法 Current_Tree() 的类中设置标签值。那应该返回一个 fruit_trees 对象,并且 btn click 方法应该设置标签值。

您实际上应该完全摆脱该方法(除非在上面的代码之外有某种原因。

于 2012-12-13T16:00:25.497 回答