请问如何获得集合中最大的那个数

dsgv45 2011-07-06 10:26:53
请问如何获得List<int>集合中,最大的那个数
...全文
253 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyerwing 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taomanman 的回复:]
C# code

List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
var result = nums.Where(p=>Max(p));
[/Quote]
max函数就可以了
kingdom_0 2011-07-07
  • 打赏
  • 举报
回复

集合有Max方法的,直接调用就行了。
List<int> nums = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int maxValue= nums.Max();
暖枫无敌 2011-07-07
  • 打赏
  • 举报
回复
不好意思,昨天随便写的,没测试(LINQ语法陌生了,好久不用),下面这个没有问题了,测试过的。

List<int> nums = new List<int>{1,2,3,100,4,5,6,7,8,9,10};
var result = nums.Select(p=>p).Max();
Response.Write("最大值为:"+result .ToString());
isjoe 2011-07-07
  • 打赏
  • 举报
回复
写比较接口。。这个方法兼容性好
dsgv45 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 taomanman 的回复:]
这个是LINQ语法,你的VS是多少的?
[/Quote]
-----"当前上下文不存在Max " ?
huangwenquan123 2011-07-06
  • 打赏
  • 举报
回复
    
//按从大到小排序后取最第一个
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
list.Sort(new CompareMax());
Console.WriteLine(list[0]);
Console.ReadLine();
}
}
public class CompareMax : IComparer<int>
{
public int Compare(int x, int y)
{
return y - x;
}
}
dsgv45 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 taomanman 的回复:]
这个是LINQ语法,你的VS是多少的?
[/Quote]
2010
暖枫无敌 2011-07-06
  • 打赏
  • 举报
回复
这个是LINQ语法,你的VS是多少的?
暖枫无敌 2011-07-06
  • 打赏
  • 举报
回复

List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
var result = nums.Where(p=>p).Max();

foreach(int i in result)
{
Response.Write("最大值为:"+i.ToString());
}
hanfeng_st 2011-07-06
  • 打赏
  • 举报
回复
循环比较 查找
dsgv45 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taomanman 的回复:]
C# code

List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
var result = nums.Where(p=>Max(p));
[/Quote]
是不是有什么命名空间-----"当前上下文不存在Max "
dsgv45 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taomanman 的回复:]
C# code

List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
var result = nums.Where(p=>Max(p));
[/Quote]
Link查询 ?
暖枫无敌 2011-07-06
  • 打赏
  • 举报
回复

List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
var result = nums.Where(p=>Max(p));

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧