2012-12-13 87 views
-1

我正在處理5個項目的鏈接列表。我創建了包含項目列表的fruit_trees[] ax。我收到ListForTrees newlist = new ListForTrees(ax);中的錯誤,表示它有無效的參數。我不確定這個錯誤。我怎樣才能正確地通過構造函數傳遞數組?鏈接列表:顯示項目

namespace treeFarm 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public class fruit_trees 
     { 
      private string tree_type = " "; 
      private int tree_height = 0; 
      public double tree_price = 0; 
      private int tree_instock = 0; 


      public fruit_trees next_tree; 

      public fruit_trees(string newtree, int newheight, double newprice, int newinstock) 
      { 
       tree_type = newtree; 
       tree_height = newheight; 
       tree_price = newprice; 
       tree_instock = newinstock; 


       next_tree = null; 
      } 

      public override string ToString() 
      { 
       return tree_type + " " + tree_height + " " + tree_price + " " + tree_instock; 

      } 

     } 


     public class ListForTrees 
     { 
      public fruit_trees first_tree; 
      public fruit_trees last_tree; 

      public int count = 0; 

      public ListForTrees(fruit_trees new_tree) 
      { 
       first_tree = new_tree; 
       last_tree = new_tree; 
       count = 1; 
      } 

      public ListForTrees() 
      { 

      } 

      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++; 
       } 
      } 

      public void ClearTrees() 
      { 
       first_tree = null; 
       count = 0; 
      } 
     } 

     ListForTrees mainlist = new ListForTrees(); 


     private void ShowList_Click(object sender, EventArgs e) 
     { 
      fruit_trees current = mainlist.first_tree; 

      while (current != null) 
      { 

       TxtOutput.AppendText(current.ToString() + Environment.NewLine);   
       current = current.next_tree; 
      } 

      TxtOutput.AppendText(Environment.NewLine); 
     } 


     private void BtnGo_Click(object sender, EventArgs e) 
     { 
      fruit_trees[] ax = { new fruit_trees("item1", 2, 2.00, 3), 
              new fruit_trees("item2", 2, 3.95, 6), 
              new fruit_trees("item3", 5, 9.95, 2), 
              new fruit_trees("item4", 54, 19.95, 3), 
              new fruit_trees("item5", 36, 11.85, 2), 
              new fruit_trees("item6", 62, 13.45, 5) 
             }; 

      ListForTrees newlist = new ListForTrees(ax); 
      fruit_trees current = newlist.first_tree; 

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

回答

2

您沒有一個構造函數,它需要一個fruit_trees實例的數組。你可能想從另外一個初始化你的收藏,讓你可以添加一個構造函數中添加的每個元素:

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

謝謝你,完美的解釋。 – techAddict82

0

真的好像很直白。您的ListForTrees構造函數接受類型爲fruit_trees的參數,但是您傳遞的是類型爲fruit_trees[]的參數。