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

Arrays.copyOfRange not working as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u5
    • core-libs
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      1.8.0_05

      ADDITIONAL OS VERSION INFORMATION :
      Win7 version 6.1.7601

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      not really needed, just doing algorithm exercises

      A DESCRIPTION OF THE PROBLEM :
      in Arrays.java of the utils package there is a method:

      public static int[] copyOfRange(int[] original, int from, int to) {
              int newLength = to - from;
              if (newLength < 0)
                  throw new IllegalArgumentException(from + " > " + to);
              int[] copy = new int[newLength];
              System.arraycopy(original, from, copy, 0,
                               Math.min(original.length - from, newLength));
              return copy;
          }

      if I put that I want to only copy the 0 index, so I call the copyOfRange(myOriginalArray, 0, 0); the next thing it does is try to make the length of the copy array 0 because the logic gets the length from subtracting the indexes you enter here. The difference of 1 between array length and array indexes seems to be overlooked here.
      I changed the first two lines to:

      int newLength = (to - from) + 1;
      if (newLength <= 0)

      that way it will count the length you want correctly and it will still throw the exception at the correct time as well.

      ADDITIONAL REGRESSION INFORMATION:
      I don't know if this is present in other releases

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      just try to use Arrays.copyOfRange from the utils package

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected calling copyOfRange(myArray, 0, 0); would return an array with a length of 1
      ACTUAL -
      calling copyOfRange(myArray,0 ,0); returns an empty array.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      none it compiles fine

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      int array[] = {10, 12, 13, 4};
      int copyArray[] = Arrays.copyOfRange(array, 0, 0);
      System.out.println(tempArray[0]);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      public static int[] copyOfRange(int[] original, int from, int to) {
              int newLength = (to - from) + 1;
              if (newLength <= 0)
                  throw new IllegalArgumentException(from + " > " + to);
              int[] copy = new int[newLength];
              System.arraycopy(original, from, copy, 0,
                               Math.min(original.length - from, newLength));
              return copy;
          }

      just change the first two lines of the method like I have:

      int newLength = (to - from) + 1;
              if (newLength <= 0)

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: