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




相关文章
|
C语言
C语言之给定n个数据, 求最大值出现的位置(如果最大值出现多次,求出第一次出现的位置即可,位置从1开始)。
C语言之给定n个数据, 求最大值出现的位置(如果最大值出现多次,求出第一次出现的位置即可,位置从1开始)。
346 0
|
8天前
|
搜索推荐 Java 数据处理
数组筛选,将数组[2,0,6,1,77,0,52,0,25,7]中大于等于10元素选出来,放入新数组,声明一个新的数组用于存放新数据newArr,遍历原来的旧数组,找到大于10的元素,依次追加新数组
数组筛选,将数组[2,0,6,1,77,0,52,0,25,7]中大于等于10元素选出来,放入新数组,声明一个新的数组用于存放新数据newArr,遍历原来的旧数组,找到大于10的元素,依次追加新数组
|
5月前
|
机器学习/深度学习 算法 数据处理
盘点四种计算数组中元素值为1的个数的方法
盘点四种计算数组中元素值为1的个数的方法
63 0
|
5月前
|
存储 算法 Java
Java:查找一个给定数组中的最大值和最小值
Java:查找一个给定数组中的最大值和最小值
|
5月前
|
算法 索引 Python
如何实现二分查找算法? 要求:编写一个Python函数,输入一个有序列表和一个目标值,返回目标值在列表中的索引。如果目标值不在列表中,返回-1。
如何实现二分查找算法? 要求:编写一个Python函数,输入一个有序列表和一个目标值,返回目标值在列表中的索引。如果目标值不在列表中,返回-1。
66 0
定义一个包含10个整数元素的数组,初始值由用户给定。找出数组中的最大数并连同下标一起输出。
定义一个包含10个整数元素的数组,初始值由用户给定。找出数组中的最大数并连同下标一起输出。
208 0
|
存储
返回集合中最大,最小的元素,再将元素进行排序
返回集合中最大,最小的元素,再将元素进行排序
58 0
|
算法 前端开发
前端算法-查找旋排序数组中最小值
前端算法-查找旋转排序数组中最小值