java findindex,LIST.FindIndex(predicate<T> match)的参数有关问题

LIST.FindIndex(predicate match)的参数问题

List.FindIndex(predicate match)的参数是怎么写啊?这个看不懂。。。

谁能帮我

//删除一个教师记录

public static void Delete()

{

//根据工号查询,是否存在该记录,如不存在则提示,存在则删除

teacherID = Console.ReadLine(); //输入工号

//根据teacherID查询集合类  1.foreach遍历整个集合  2.List类提供的FindIndex方法

int index1 = teacherList.FindIndex(teacherID);//----错误:参数不正确

if (index1 = (-1))

{

Console.WriteLine("没有此工号!");

}

else

{

teacherList.RemoveAt(index1);

}

上面代码FindIndex参数怎么写呢?

好心人,帮帮忙吧!

分享到:

更多

------解决方案--------------------

teacherList的定义呢?

predicate这样的泛型委托一般采用lambda表达式的形式来表示,形式是

i => i == teacherID

这里假定

var teacherList = new List();

------解决方案--------------------

predicate是个delegate,定义为:

public delegate bool Predicate (T obj)

只要是个函数,或者匿名函数,符合上面的声明即可。

下面是msdn中List.FindIndex的例子,来自:

http://msdn.microsoft.com/zh-cn/library/x1xzf2ca(v=vs.80).aspx

using System;

using System.Collections.Generic;

public class Example

{

public static void Main()

{

List dinosaurs = new List();

dinosaurs.Add("Compsognathus");

dinosaurs.Add("Amargasaurus");

dinosaurs.Add("Oviraptor");

dinosaurs.Add("Velociraptor");

dinosaurs.Add("Deinonychus");

dinosaurs.Add("Dilophosaurus");

dinosaurs.Add("Gallimimus");

dinosaurs.Add("Triceratops");

Console.WriteLine();

foreach(string dinosaur in dinosaurs)

{

Console.WriteLine(dinosaur);

}

Console.WriteLine("\nFindIndex(EndsWithSaurus): {0}",

dinosaurs.FindIndex(EndsWithSaurus));

Console.WriteLine("\nFindIndex(2, EndsWithSaurus): {0}",

dinosaurs.FindIndex(2, EndsWithSaurus));

Console.WriteLine("\nFindIndex(2, 3, EndsWithSaurus): {0}",

dinosaurs.FindIndex(2, 3, EndsWithSaurus));

}

// Search predicate returns true if a string ends in "saurus".

private static bool EndsWithSaurus(String s)

{

if ((s.Length > 5) &&

(s.Substring(s.Length - 6).ToLower() == "saurus"))

{

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值