集册 Java实例教程 以数组形式返回变量参数nums中所有数字的最小值和最大值。

以数组形式返回变量参数nums中所有数字的最小值和最大值。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

522
以数组形式返回变量参数nums中所有数字的最小值和最大值。


public class Main {


  /**

   * Return the minimum and maximum of all numbers in the variable argument nums

   * as an array. Please see java.lang.Number for understanding properties of

   * Number. Your program should pass the following tests:

   * <ul>

   * <li>minMax() return {}

   * <li>minMax(3) return {3D,3D}.

   * <li>minMax(10, 20D, 3) return {3D, 20D}

   * <li>mainMax(new BigInteger("200"), 20D, -3L) return {-3D, 200D}.

   * </ul>

   *

   * @param nums

   *          a array of Numbers.

   * @return a double array with minimum value put at position 0 and maximum value

   *         put at position 1 or an empty array if there is no input.

   *//** 时代Java 提 供 **/

  @SafeVarargs

  public static <N extends Number> double[] minMax(N... nums) {

    // replace following code by yours.

    double[] ans;


    if (nums.length == 0) {

      ans = new double[0];

    } else {

      ans = new double[2];


      ans[0] = ans[1] = nums[0].doubleValue();


      for (Number t : nums) {

        
展开阅读全文