Dart列表最小/最大值 [英] Dart List min/max value

查看:2276
本文介绍了Dart列表最小/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取Dart中List的最小值和最大值。

  1,2,3,4,5] .min //返回1 
[1,2,3,4,5] .max //返回5
pre>

我相信我可以 写一个简短的函数或 b)并选择最后一个值,



,但我正在寻找是否有更多的本地解决方案,如果有任何。

解决方案

假设列表不为空,您可以使用 Iterable.reduce

  import'dart:math'; 

main(){
print([1,2,8,6] .reduce(max)); // 8
print([1,2,8,6] .reduce(min)); // 1
}


How do you get the min and max values of a List in Dart.

[1, 2, 3, 4, 5].min //returns 1
[1, 2, 3, 4, 5].max //returns 5

I'm sure I could a) write a short function or b) copy then sort the list and select the last value,

but I'm looking to see if there is a more native solution if there is any.

解决方案

Assuming the list is not empty you can use Iterable.reduce :

import 'dart:math';

main(){
  print([1,2,8,6].reduce(max)); // 8
  print([1,2,8,6].reduce(min)); // 1
}

这篇关于Dart列表最小/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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