📌  相关文章
📜  Dart – 在列表中查找最小值和最大值

📅  最后修改于: 2021-09-02 05:33:27             🧑  作者: Mango

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

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

使用for 循环

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

例子:

Dart
// 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}");
}


Dart
// 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");
}


Dart
// 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
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");
}


Dart
// 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
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");
}


输出:

Smallest value in the list : 3
Largest value in the list : 121

使用排序函数

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

例子:

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}");
}

输出:

Smallest value in the list : 3
Largest value in the list : 121

使用forEach 循环

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

例子:

Dart

// 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");
}

输出:

Smallest value in the list : 3
Largest value in the list : 121

使用reduce方法

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

例子:

Dart

// 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");
}

输出:

Smallest value in the list : 3
Largest value in the list : 121

dart使用reduce 方法:数学

上面的代码可以通过导入数学库来最小化。

例子:

Dart

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");
}

输出:

Smallest value in the list : 3
Largest value in the list : 121

使用折叠方法

除了reduce dart还有fold方法,它与reduce非常相似,只是它从一个初始值开始,然后在这个过程中改变它。

例子:

Dart

// 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");
}

输出:

Smallest value in the list : 3
Largest value in the list : 121

dart使用fold 方法:数学

上面的代码可以通过导入数学库来最小化。

例子:

Dart

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");
}

输出:

Smallest value in the list : 3
Largest value in the list : 121