如何从列表中查找倒数第二个元素? [英] How to find second last element from a List?

查看:763
本文介绍了如何从列表中查找倒数第二个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<string>,例如:

 List<String> lsRelation = new List<String>{"99","86","111","105"}.

现在,我想找到倒数第二个字符串Number 111.

Now i want to find the Number 111 that is the second to last string.

所以我尝试过:

String strSecondLast=lsrelation.Last() - 2;

这不起作用.因此,如何使用Last()查找列表的倒数第二个元素.

which doesn't work. So how can i find the second to last element of a List using Last().

推荐答案

使用:

if (lsRelation.Count >= 2)
    secLast = lsRelation[lsRelation.Count - 2];

这篇关于如何从列表中查找倒数第二个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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