我正在處理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;
}
}
}
}
謝謝你,完美的解釋。 – techAddict82