在 C# 中搜索匹配给定条件的元素并返回整个 List 中最后一次出现的基于 0 的索引
搜索匹配条件的元素并返回整个 List 中最后一次出现的基于 0 的索引的代码如下 −
示例
using System;
using System.Collections.Generic;
public class Demo {
private static bool demo(int i) {
return ((i % 10) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(200);
list.Add(215);
list.Add(310);
list.Add(500);
list.Add(600);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine("Last Index of the satisfying condition = "+list.FindLastIndex(demo));
}
}输出
输出结果如下 −
List elements... 200 215 310 500 600 Last Index of the satisfying condition = 4
示例
下面我们来看另一个示例 −
using System;
using System.Collections.Generic;
public class Demo {
private static bool demo(int i) {
return ((i % 2) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(200);
list.Add(215);
list.Add(310);
list.Add(500);
list.Add(655);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine("Last Index of the satisfying condition = "+list.FindLastIndex(demo));
}
}输出
输出结果如下 −
List elements... 200 215 310 500 655 Last Index of the satisfying condition = 3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP