我如何才能找到一个List&LT的最后一个元素;> ? [英] How can I find the last element in a List<> ?

查看:138
本文介绍了我如何才能找到一个List&LT的最后一个元素;> ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码的摘录:

The following is an extract from my code:

public class AllIntegerIDs 
{
    public AllIntegerIDs() 
    {            
        m_MessageID = 0;
        m_MessageType = 0;
        m_ClassID = 0;
        m_CategoryID = 0;
        m_MessageText = null;
    }

    ~AllIntegerIDs()
    {
    }

    public void SetIntegerValues (int messageID, int messagetype,
        int classID, int categoryID)
    {
        this.m_MessageID = messageID;
        this.m_MessageType = messagetype;
        this.m_ClassID = classID;
        this.m_CategoryID = categoryID;
    }

    public string m_MessageText;
    public int m_MessageID;
    public int m_MessageType;
    public int m_ClassID;
    public int m_CategoryID;
}



我想用我的main()函数的代码如下:

I am trying to use the following in my main() function code:

List<AllIntegerIDs> integerList = new List<AllIntegerIDs>();

/* some code here that is ised for following assignments*/
{
   integerList.Add(new AllIntegerIDs());
   index++;
   integerList[index].m_MessageID = (int)IntegerIDsSubstring[IntOffset];
   integerList[index].m_MessageType = (int)IntegerIDsSubstring[IntOffset + 1];
   integerList[index].m_ClassID = (int)IntegerIDsSubstring[IntOffset + 2];
   integerList[index].m_CategoryID = (int)IntegerIDsSubstring[IntOffset + 3];
   integerList[index].m_MessageText = MessageTextSubstring;
}



问题是在这里:我尝试使用打印的所有元素在我的列表循环:

Problem is here: I am trying to print all elements in my List using a for loop:

for (int cnt3 = 0 ; cnt3 <= integerList.FindLastIndex ; cnt3++) //<----PROBLEM HERE
{
   Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\n", integerList[cnt3].m_MessageID,integerList[cnt3].m_MessageType,integerList[cnt3].m_ClassID,integerList[cnt3].m_CategoryID, integerList[cnt3].m_MessageText);
}



我想找到的最后一个元素,让我画上等号,我对循环CNT3并在列表打印出的所有条目。列表中的每个元素是与上述代码示例中提到的类AllIntegerIDs的对象。 ?如何在列表中找到最后的有效输入

I want to find the last element so that I equate cnt3 in my for loop and print out all entries in the List. Each element in the list is an object of the class AllIntegerIDs as mentioned above in the code sample. How do I find the last valid entry in the List?

我应该使用类似integerList.Find(integerList [] m_MessageText == NULL;

Should I use something like integerList.Find(integerList[].m_MessageText == null;

如果我使用的,它需要一个范围从0到任何最大的指数。意味着我将不得不使用另外一个for循环,我不打算使用,是否有一个较短/更好的办法?

If I use that it will need an index that will range from 0 to whatever maximum. Means I will have to use another for loop which I do not intend to use. Is there a shorter/better way?

谢谢,
Viren

Thanks, Viren

推荐答案

如果你只是想访问列表的最后一个项目,你可以做

If you just want to access the last item in the list you can do

var item = integerList[integerList.Count - 1];

要获得项目列表中的总数就可以使用Count属性格式

to get the total number of items in the list you can use the Count propery

var itemCount = integerList.Count;

这篇关于我如何才能找到一个List&LT的最后一个元素;&GT; ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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