将项目附加到现有列表

时间:2012-12-13 05:39:12

标签: c#

我正在使用链接列表。我已经将我的构造函数设置为使用一组已定义的项来获取名为ax的数组。我还决定让一个输入框通过BtnAddTree_Click将新项目附加到列表ax。但不是附加到列表ax,而是创建一个全新的单独列表。如何通过我的ax函数将项目附加到数组列表AddTree

            public ListForTrees(IEnumerable<fruit_trees> trees)
            {
                foreach (fruit_trees t in trees)
                {
                    this.AddTree(t);
                }
            }



            public void AddTree(fruit_trees new_tree)
            {
                fruit_trees current = first_tree;

                if (count == 0)
                {
                    first_tree = new_tree;
                    last_tree = new_tree;
                    count = 1;
                }

                else if (count != 0)
                {
                    if (new_tree.tree_price <= first_tree.tree_price)
                    {
                        new_tree.next_tree = first_tree;
                        first_tree = new_tree;
                    }
                    else if (new_tree.tree_price >= last_tree.tree_price)
                    {
                        last_tree.next_tree = new_tree;
                        last_tree = new_tree;
                    }
                    else
                    {
                        while (new_tree.tree_price > current.next_tree.tree_price)
                        {
                            current = current.next_tree;
                        }
                        new_tree.next_tree = current.next_tree;
                        current.next_tree = new_tree;
                    }
                    count++;
                }
            }

        }

        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)
            {                   
                current = current.next_tree;             
            }

        }
    }
}

1 个答案:

答案 0 :(得分:2)

它似乎没有创建一个新的单独列表。我用你的代码测试了下面的代码:

public class TreeTester
{
  public static void Main(string[] args)
  {
    var list = new ListForTrees(
        new[] { new fruit_trees("tree10",10,10,10), new fruit_trees("tree2",2,2,2) });

    list.AddTree( new fruit_trees("tree3",3,3,3) );     // middle
    list.AddTree( new fruit_trees("tree1",1,1,1) );     // first
    list.AddTree( new fruit_trees("tree50",50,50,50) ); // last
    list.AddTree( new fruit_trees("tree5",5,5,5) );     // middle
    Console.Write(list);
  }
}

得到以下输出,这似乎是正确的。

tree1 1 1 1
tree2 2 2 2
tree3 3 3 3
tree5 5 5 5
tree10 10 10 10
tree50 50 50 50

如果这不正确,预期的行为是什么?显然,这些 项都被添加到原始列表中,因为当我遍历列表时它们就会出现。

顺便说一句,我还在ToString课程中添加了以下ListForTrees函数;它使调试更容易。

public override string ToString()
{
  string s = "";
  for (var tree=first_tree; tree!=null; tree = tree.next_tree)
    s += tree + "\n";
  return s;
}

编辑:我必须发表评论,您可能会发现在尝试了解错误的位置时稍微清理一下代码会有所帮助。例如,您的ListForTrees(fruit_trees new_tree)构造函数与调用Add(new_tree)完全相同。另外,考虑一下你在else if (count != 0)下添加的三个案例 - 也许有一种方法可以优雅地组合成一个通用的while循环?它使分析更容易,并且(可能)更不容易出错。