C# 链接列表:显示项目
我正在使用5个项目的链接列表。我已经创建了C# 链接列表:显示项目,c#,C#,我正在使用5个项目的链接列表。我已经创建了果树[]ax,其中包含一个项目列表。我在ListForTrees newlist=newlistfortrees(ax)中收到一个错误表示它有无效参数。我不确定这个错误。如何正确地将数组传递给构造函数 namespace treeFarm { public partial class Form1 : Form { public Form1() { InitializeComponen
果树[]ax
,其中包含一个项目列表。我在ListForTrees newlist=newlistfortrees(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;
}
}
}
}
命名空间树格式
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
公营果树
{
私有字符串树_type=“”;
私有int树的高度=0;
公共双树价格=0;
私有int树_instock=0;
公共果树旁边的果树;
公共果树(串新树、整数新高、双倍新价、整数新木)
{
树类型=新树;
树高=新高度;
树价格=新价格;
树_instock=新instock;
next_tree=null;
}
公共重写字符串ToString()
{
返回树类型+“”+树高+“”+树价格+“”+树库存;
}
}
公共类ListForTrees
{
公共果树第一棵;
公共果树最后一棵树;
公共整数计数=0;
公共列表树(果树新树)
{
第一棵树=新的树;
最后一棵树=新的树;
计数=1;
}
公共ListForTrees()
{
}
公共树木(果树新树)
{
当前果树=第一棵果树;
如果(计数=0)
{
第一棵树=新的树;
最后一棵树=新的树;
计数=1;
}
否则如果(计数!=0)
{
if(new_tree.tree_price=last_tree.tree_price)
{
最后一棵树。下一棵树=新的树;
最后一棵树=新的树;
}
其他的
{
while(new_tree.tree_price>current.next_tree.tree_price)
{
current=current.next_树;
}
new_tree.next_tree=current.next_tree;
current.next_tree=新_tree;
}
计数++;
}
}
公共树木()
{
第一棵树=null;
计数=0;
}
}
ListForTrees mainlist=新建ListForTrees();
私有无效显示列表\u单击(对象发送者,事件参数e)
{
水果树当前=主列表。第一棵树;
while(当前!=null)
{
TxtOutput.AppendText(current.ToString()+Environment.NewLine);
current=current.next_树;
}
AppendText(Environment.NewLine);
}
私有void BtnGo\u单击(对象发送者,事件参数e)
{
果树[]ax={新果树(“第1、2、2.00、3项”),
新果树(项目2,2,3.95,6),
新果树(项目3、5、9.95、2),
新果树(项目4,54,19.95,3),
新果树(项目5、36、11.85、2),
新果树(项目6、62、13.45、5)
};
ListForTrees newlist=新ListForTrees(ax);
水果树当前=newlist.first\u树;
while(当前!=null)
{
TxtOutput.AppendText(current.ToString()+Environment.NewLine);
current=current.next_树;
}
}
}
}
看起来很直截了当,真的。您的ListForTrees
构造函数接受类型为fruit\u trees
的参数,但您正在传递类型为fruit\u trees[]
的参数。您没有接受fruit\u trees
实例数组的构造函数。您可能希望从另一个集合初始化集合,以便可以添加构造函数来添加每个元素:
public ListForTrees(IEnumerable<fruit_trees> trees)
{
foreach(fruit_trees t in trees)
{
this.AddTree(t);
}
}
公共列表树(IEnumerable trees)
{
foreach(树中的果树)
{
这是AddTree(t);
}
}
谢谢,完美的解释。