精易论坛

标题: 调用方法中数组中的每一个元素 取出最小值 [打印本页]

作者: 4378430    时间: 2017-10-23 01:34
标题: 调用方法中数组中的每一个元素 取出最小值
本帖最后由 4378430 于 2017-10-23 01:43 编辑
  1. <div class="blockcode"><blockquote>package aiyu.day05;
  2. //主方法中给定数组int[] arr= {10,20,30,40,50,60};

  3. //定义一个方法可以接受这个给定的数组
  4. //并返回这个数组中元素的最小值
  5. //首先分析这个题目的三个要素
  6. //1,这个方法的作用是什么?        // 求数组中的最小值
  7. //2,要完成这个需求,我需要哪些参数 //吧数组给我
  8. //3,操作完毕之后,要不要吧结果返回出去//要返回,返回最小值,int

  9. public class Dome_3 {
  10.         // 创建main方法
  11.         public static void main(String[] args) {

  12.                 // 主方法中定义数组int[] arr= {10,20,30,40,50,60};
  13.                 int[] arr = { 10, 20, 30, 40, 50, 60 };

  14.                 // 最后一步 调用方法里面的js(名称)
  15.                 System.out.println(js(arr));
  16.         }

  17.         // 创建方法
  18.         public static int js(int[] a) {// int [] --- int类型的数组 因为返回的最小值是整数型 int类型的
  19.                 // 赋值一个a[0]的数组 后面用来输出最小值
  20.                 int min = a[0];
  21.                 // 循环遍历接受int[]arr的数组元素
  22.                 for (int i = 0; i < a.length; i++) {
  23.                         // 判断数组元素中的最小值
  24.                         if (min > a[i]) {
  25.                                 min = a[i];
  26.                         }
  27.                 }
  28.                 return min;// 返回值(返回最小值,利于后期的调用)
  29.         }

  30. }
复制代码


作者: Sir雨轩    时间: 2017-10-23 08:01
获取数组的最小值和最大值有简单的方法
int min = (int) Collections.min(Arrays.asList(arr));
int max = (int) Collections.max(Arrays.asList(arr));
作者: 4378430    时间: 2017-10-24 09:47
Sir雨轩 发表于 2017-10-23 08:01
获取数组的最小值和最大值有简单的方法
int min = (int) Collections.min(Arrays.asList(arr));
int max  ...

谢谢   这些关键词我们还没有学习到
作者: wsmwzd    时间: 2018-2-19 07:23
哎该努力学习了




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4