显示列表项的位置 [英] Show position of item in a list
问题描述
我的链接列表的工作。我的构造函数阵列,它的项目,或通过 BtnAddTree
点击追加项目(一个在时间)到列表中。对于实验的原因,我试图找出一种方法来检索项目从列表中的位置。我已经设定了三个按钮,将有望显示:第一
,接下来
,末页
项目,并显示通过标签
。我创建了两个函数来实现我的需要提取
和 Current_Tree()
,但这里是我走到了穷途末路。我知道,以显示为项目接下来
我可能需要使用一个循环,但不知道该如何去做。我怎样才能显示在列表中找到一个项目的位置?
公共类ListForTrees
{
//检索项
公共无效的位置检索(INT位置)
{
INT new_position =位置;
fruit_trees电流= first_tree;
的for(int i = 0; I<&定位功放;&安培;!当前= NULL;我++)
{
电流= current.next_tree;
}
返回电流;
}
//显示当前树
公共无效Current_Tree()
{
试
{
电流= fruit_trees。 first_tree;
labelSpecificTree.Text = current.Type.ToString();
}
赶上{};
}
}
ListForTrees mainlist =新ListForTrees();
私人无效BtnGo_Click(对象发件人,EventArgs五)
{
fruit_trees [] AX = {新fruit_trees(樱桃,48,12.95,3),
新fruit_trees(松,36岁,9.95,8),
新fruit_trees(橡木,60,14.95,2),
新fruit_trees(桃花源记,54,19.95,3) ,
新fruit_trees(鸭梨,36,11.85,2),
新fruit_trees(苹果,62,13.45,5)
};
mainlist =新ListForTrees(AX);
fruit_trees电流= mainlist.first_tree;
,而(电流!= NULL)
{
TxtOutput.AppendText(current.ToString()+ Environment.NewLine);
电流= current.next_tree;
}
}
私人无效ShowNextItem_Click(对象发件人,EventArgs五)
{
//显示下一项目
}
私人无效ShowLastItem_Click(对象发件人,EventArgs五)
{
//显示最后项目
}
私人无效ShowFirstItem_Click_1(对象发件人,EventArgs E)
{
//显示第一个项目
}
}
}
开发了一种正确的比较对象。是树型独特之处?也许你需要一个完整的IComparable的?
然后创建在listoftrees类中的方法
公众诠释为getPosition(fruit_trees树)
{
fruit_trees英尺= first_tree;
如果(ft.tree_type == tree.tree_type)
{
返回0; //或返回1这取决于你如何想索引的东西
}
INT I = 1; //如果可能高于
在返回设置为1到2(!ft.next_tree = NULL)
{
FT = ft.next_tree;
如果(ft.tree_type == tree.tree_type)
{
回报我;
}
I ++;
}
返回-1; //或别的东西在这里显示,其不在列表
}
也可以作为一个侧面说明..Dont设置在类的标签值的方法Current_Tree()。这应该返回fruit_trees对象和BTN点击方法应设置标签值。
您实际上应该刚刚摆脱这种方法完全(除非有一些理由吧上述以外的代码。
I am working with a link list. My constructor takes arrays with items in it or appends an item(one at time) to the list through BtnAddTree
click. For experimental reasons I am trying to figure out a way to retrieve the position of an item from the list. I have set three buttons that will hopefully show: first
, next
, last
item and display through a label
. I created two functions to achieve my need Retrieve
and Current_Tree()
but here is where I come to a dead end. I am aware to display items that are next
I may need to use a loop but unsure how to go about it. How can I display the position of an item found in the list?
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
}
}
}
Develop a way to compare the object properly. is tree type unique? maybe you need a full icomparable?
then create a method in the listoftrees class
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
}
also as a side note ..Dont set the label value in the class in the method Current_Tree(). That should return a fruit_trees object and the btn click methods should set the label value .
you should actually just get rid of that method completely(unless there is some reason for it outside of the code above.
这篇关于显示列表项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!