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

Clarify javadoc for java.util.Collections.copy()

    Details

    • Subcomponent:
    • Resolved In Build:
      b88

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.8.0_60"
        Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
        Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        ver Windows 8.1

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        this stuff is not relevant

        A DESCRIPTION OF THE PROBLEM :
        1.public static <T> void copy(List<? super T> DEST, List<? extends T> SRC) {
        2. int srcSize = src.size();
        3. if (srcSize > dest.size())
        4. throw new IndexOutOfBoundsException("Source does not fit in dest");

        ........

        This method does not work if you want to copy list into empty (does not mean with capacity 0) DEST.
        It's possible to create DEST list with any initial capacity, but if it's empty - method size() returns 0 which means if I want to copy my SRC list into empty DEST list - operation fails because line 3 is wrong srcSize will be > 0.
        Please update javadoc which should say that DEST list must contains at least the same number of elements as SRC OR fix the implemenation !

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        List<Any_Object> dest = new ArrayList<>(100); // size 0
        List<Any_Object> src= new ArrayList<>(); // size 0
        src.add(new Any_Object()); // size 1

        java.util.Collections.copy(dest, src); // fails

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        It must be possible to copy 1 list into another empty list.
        ACTUAL -
        It is not possible to copy 1 list into another empty list.

        REPRODUCIBILITY :
        This bug can be reproduced always.

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  igerasim Ivan Gerasimov
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  6 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: