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

Range checks are unnecessary in Arrays.fill() when filling an entire array

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 7
    • 6
    • core-libs
    • b17
    • generic
    • generic
    • Not verified

    Description

      Arrays.fill() methods that fill an entire array, e.g.,

          public static void fill(long[] a, long val) {
              fill(a, 0, a.length, val);
          }

      call the from/to-index form, e.g.,

          public static void fill(long[] a, int fromIndex, int toIndex, long val) {
              rangeCheck(a.length, fromIndex, toIndex);
              for (int i=fromIndex; i<toIndex; i++)
                  a[i] = val;
          }

      which call rangeCheck(), vis,

          private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
              if (fromIndex > toIndex)
                  throw new IllegalArgumentException("fromIndex(" + fromIndex +
                             ") > toIndex(" + toIndex+")");
              if (fromIndex < 0)
                  throw new ArrayIndexOutOfBoundsException(fromIndex);
              if (toIndex > arrayLen)
                  throw new ArrayIndexOutOfBoundsException(toIndex);
          }

      When filling an entire array, none of the exceptions in rangeCheck can happen,
      because fromIndex == 0 and toIndex == arrayLen and is always >= 0.

      For these fill() methods, the call to rangeCheck can be elided.

      Attachments

        Issue Links

          Activity

            People

              martin Martin Buchholz
              phh Paul Hohensee
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: