Dart 中找到给定列表中存在的最小值和最大值元素

简介: 在 Dart 中,我们可以通过七种方式找到给定列表中存在的最小值和最大值元素:使用for 循环查找最大和最小元素。使用sort 函数查找最大和最小元素。使用forEach 循环查找最大和最小元素。在 dart 中仅使用reduce 方法来查找最大和最小元素。在dart:math库中使用reduce 方法。使用带有 dart 的fold 方法来查找最大和最小元素。在dart:math库中使用fold 方法。

在 Dart 中,我们可以通过七种方式找到给定列表中存在的最小值和最大值元素:

  1. 使用for 循环查找最大和最小元素。
  2. 使用sort 函数查找最大和最小元素。
  3. 使用forEach 循环查找最大和最小元素。
  4. 在 dart 中仅使用reduce 方法来查找最大和最小元素。
  5. 在dart:math库中使用reduce 方法。
  6. 使用带有 dart 的fold 方法来查找最大和最小元素。
  7. 在dart:math库中使用fold 方法。

使用for 循环

这是查找列表中最大和最小元素的最基本方法,只需遍历所有元素并进行比较并给出答案。

例子:

// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Declaring and assigning the
// largestGeekValue and smallestGeekValue
var largestGeekValue = geekList[0];
var smallestGeekValue = geekList[0];
for (var i = 0; i < geekList.length; i++) {
    // Checking for largest value in the list
    if (geekList[i] > largestGeekValue) {
    largestGeekValue = geekList[i];
    }
    // Checking for smallest value in the list
    if (geekList[i] < smallestGeekValue) {
    smallestGeekValue = geekList[i];
    }
}
// Printing the values
print("Smallest value in the list : $smallestGeekValue");
print("Largest value in the list : $largestGeekValue");
}

使用排序功能

Dart 还允许用户按升序对列表进行排序,即第一个最小,最后一个最大。

例子:*

// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Sorting the list
geekList.sort();
// Printing the values
print("Smallest value in the list : ${geekList.first}");
print("Largest value in the list : ${geekList.last}");
}

使用forEach 循环

与 for 循环不同,还可以使用 forEach 循环来获取列表元素,然后检查列表元素中的条件。

// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Declaring and assigning the
// largestGeekValue and smallestGeekValue
var largestGeekValue = geekList[0];
var smallestGeekValue = geekList[0];
// Using forEach loop to find
// the largest and smallest
// numbers in the list
geekList.forEach((gfg) => {
        if (gfg > largestGeekValue) {largestGeekValue = gfg},
        if (gfg < smallestGeekValue) {smallestGeekValue = gfg},
    });
// Printing the values
print("Smallest value in the list : $smallestGeekValue");
print("Largest value in the list : $largestGeekValue");
}

使用reduce方法

可以使用 Dart reduce函数将列表减少到特定值并保存。

// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Declaring and assigning the
// largestGeekValue and smallestGeekValue
// Finding the smallest and largest
// value in the list
var smallestGeekValue = geekList.reduce(
(current, next) => current < next ? current : next);
var largestGeekValue = geekList.reduce(
(current, next) => current > next ? current : next);
// Printing the values
print("Smallest value in the list : $smallestGeekValue");
print("Largest value in the list : $largestGeekValue");

在dart:math库中使用reduce方法

import "dart:math";
// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Declaring and assigning the
// largestGeekValue and smallestGeekValue
// Finding the smallest and largest value in the list
var smallestGeekValue = geekList.reduce(min);
var largestGeekValue = geekList.reduce(max);
// Printing the values
print("Smallest value in the list : $smallestGeekValue");
print("Largest value in the list : $largestGeekValue");

使用fold 方法

除了减少飞镖还有折叠,它与减少非常相似,只是从一个初始值开始,然后在这个过程中改变它。

例子:

// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Declaring and assigning the
// largestGeekValue and smallestGeekValue
// Finding the smallest and
// largest value in the list
var smallestGeekValue = geekList.fold(geekList[0],
(previous, current) => previous < current ? previous : current);
var largestGeekValue = geekList.fold(geekList[0],
(previous, current) => previous > current ? previous : current);
// Printing the values
print("Smallest value in the list : $smallestGeekValue");
print("Largest value in the list : $largestGeekValue");
}

在dart:math库中使用fold 方法

import "dart:math";
// Main function
void main() {
// Creating a geek list
var geekList = [121, 12, 33, 14, 3];
// Declaring and assigning
// the largestGeekValue and smallestGeekValue
// Finding the smallest and
// largest value in the list
var smallestGeekValue = geekList.fold(geekList[0],min);
var largestGeekValue = geekList.fold(geekList[0],max);
// Printing the values
print("Smallest value in the list : $smallestGeekValue");
print("Largest value in the list : $largestGeekValue");
}

1




相关文章
|
前端开发 JavaScript Java
揭开 JavaScript 垃圾回收的秘密——一场与内存泄漏的生死较量,让你的代码从此焕然一新!
【8月更文挑战第23天】本文通过多个实例深入探讨了JavaScript中的垃圾回收机制及其对应用性能的影响。首先介绍了基本的内存管理方式,随后分析了变量不再使用时的回收过程。接着,通过事件监听器未被移除及全局变量管理不当等场景展示了常见的内存泄漏问题。最后,文章介绍了使用`WeakRef`和`FinalizationRegistry`等现代API来有效避免内存泄漏的方法。理解并运用这些技术能显著提升Web应用的稳定性和效率。
419 0
python基础1——数值类型
python基础1——数值类型
python基础1——数值类型
|
机器学习/深度学习 数据可视化
机器学习:线性判别分析从理论到公式推导(LDA)
机器学习:线性判别分析从理论到公式推导(LDA)
529 0
机器学习:线性判别分析从理论到公式推导(LDA)
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8055 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18251 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
773 0
|
8天前
|
云安全 数据采集 人工智能