Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8276197

Update Math.max(...) and Math.min(...) to support variable parameters

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      suggested code is for all runtimes, all environments.

      A DESCRIPTION OF THE PROBLEM :
      Currently Math.max(...) and Math.min(...) support two parameters. The looks like below:
      public static int max(int a, int b) {
        return a > b ? a : b;
      }

      I suggest it be changed to:

      public static int max(int... nums) {
         int result = nums[0];
         for(int num : nums) {
            result = result > num ? result : num;
         }
         return result;
      }

      and similar code for Math.min(...).

      This will greatly help in coding challenges where min/max needs to be computed of more than 2 numbers, and since this code is backwards compatible, it will not required any changes to old code.


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: