确定当前指向的项目列表中的 [英] Determining the currently pointed to item in a list

查看:67
本文介绍了确定当前指向的项目列表中的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理列表。我已经能够确定在我的列表项的第一个和最后一个位置。我使用 getPostion 并通过标签显示项目名称。在我的表格三个按钮: ShowFirstItem ShowNextItem (没有工作)和 ShowLastItem 显示在标签中的相应项目。我有我的下一个项目的问题。我没有持有该电流 fruit_tree 的成员。所以,我不知道如何可以添加一个 INT 成员或名为当前另一个 fruit_tree 成员。我怎么能找到下一个(第一个后项),并显示结果?



 公共类ListForTrees 
{

公共fruit_trees GetNextTree()
{
电流=​​ 0;
fruit_trees英尺= first_tree;
INT I = 0;
,而(I =电流!)
{
FT = ft.next_tree;
I ++;

}

返回英尺;
}

}

ListForTrees mainlist =新ListForTrees();

私人无效BtnGo_Click(对象发件人,EventArgs五)
{
fruit_trees [] ar_items = {新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(ar_items);
fruit_trees电流= mainlist.first_tree;

,而(电流!= NULL)
{
TxtOutput.AppendText(current.ToString()+ Environment.NewLine);
电流= current.next_tree;
}
}


私人无效ShowFirstItem_Click_1(对象发件人,EventArgs五)
{
//显示第一个项目
labelSpecificTree.Text = mainlist.first_tree.GetTreeType;

}

私人无效ShowLastItem_Click(对象发件人,EventArgs五)
{
//显示最后项目
labelSpecificTree.Text = mainlist .last_tree.GetTreeType;
}

私人无效ShowNextItem_Click(对象发件人,EventArgs五)
{
//显示下一项目
fruit_trees OBJ = mainlist.GetNextTree();

如果(OBJ == NULL)
{
labelSpecificTree.Text =没有更多的树!
}
,否则
{
mainlist.current ++;
labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();
}
}



}


< DIV CLASS =h2_lin>解决方案

我想补充

 公众诠释电流; 



到listoftrees。在构造函数中设置为0;



然后创建返回当前fruit_trees对象

公共fruit_trees getCurrent()
{
fruit_trees英尺= first_tree;
INT I = 0;
,而(I =电流!)
{
FT = ft.next_tree;
I ++;
}
返回英尺;
}

现在这个方法返回当前,而不是下一个对象。所以,你必须在接下来的按钮事件2选项。结果
的quesiton这里是你想每次按钮被点击时移动到下一个树对象?
如果因此增加当前属性,然后调用getcurrent显示。如果您想当前要保留不动(即点击下一个遍地会导致同样的事情)
,则显示getcurrent()。没有增量当前next_tree.tree_type。


I am working with lists. I have been able to determine the first and last position of items in my list. I am using getPostion and displaying item name through a Label. Three buttons in my form: ShowFirstItem ShowNextItem(not working) and ShowLastItem show the corresponding item in a label. I am having problems for my next item. I don't have a member that is holding the current fruit_tree. So I am not sure how to either add an int member or another fruit_tree member called current. How would I be able to find out next (item after first) and display result?

 public class ListForTrees
 {

        public fruit_trees GetNextTree()
            {
                current = 0;
                fruit_trees ft = first_tree;
                int i = 0;
                while (i != current)
                {
                    ft = ft.next_tree;
                    i++;

                }

                return ft;
            }

            }

            ListForTrees mainlist = new ListForTrees();     

            private void BtnGo_Click(object sender, EventArgs e)
            {
                fruit_trees[] ar_items = {   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(ar_items);
                fruit_trees current = mainlist.first_tree;   

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


            private void ShowFirstItem_Click_1(object sender, EventArgs e)
            {
            // Show First Item
        labelSpecificTree.Text = mainlist.first_tree.GetTreeType;

            } 

            private void ShowLastItem_Click(object sender, EventArgs e)
            {
            //Show Last Item
        labelSpecificTree.Text = mainlist.last_tree.GetTreeType;
            }

        private void ShowNextItem_Click(object sender, EventArgs e)
            {
            //Show Next Item
 fruit_trees obj = mainlist.GetNextTree();

            if (obj == null)
            {
                labelSpecificTree.Text = "No more trees!";
            }
            else
            {
                mainlist.current++;
                labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();                
            }   
            }



        }

解决方案

I would add

public int current; 

to the listoftrees. in the constructor set it to 0;

Then create a method to return the current fruit_trees object

public fruit_trees getCurrent()
{
    fruit_trees ft = first_tree;
    int i = 0;   
    while(i != current)
    {
       ft = ft.next_tree;
       i++;
    }
    return ft;
 }

now this method returns the current and not the next object. So you have 2 options in the next button event.
the quesiton here is do you want to move to the next tree object every time the button is clicked? if so increment the current property and then call getcurrent to display. If you want the current to be retained and not move (i.e. clicking next over and over will result in the same thing) then display getcurrent().next_tree.tree_type with no increment to current.

这篇关于确定当前指向的项目列表中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆