Is Collections.copy broken (in OpenJDK 6)?

Sometimes I seem pretty inefficient. When I almost finished typing this, I managed to find the appropriate bug report in Sun. I thought well then, I could also publish it, having a screenshot and all that. The answer follows.

I know: "SELECT is not broken," and it is always my fault. But here I really don't understand why this should be. My code snippet:

List<IGraphEdge> rgSrc = this._rgGetPath();
List<IGraphEdge> rgDst = new ArrayList<IGraphEdge>(rgSrc.size());
Collections.copy(rgDst, rgSrc);

This produces a message IndexOutOfBoundExceptionwith a message

java.lang.IndexOutOfBoundsException: Source does not fit in dest
        at java.util.Collections.copy (Collections.java∗48)

In the debugger, when I enter Collections.copy, two instances ArrayListlook like this:

Debugger View http://static.theuprightape.net/ql/img/debugger.png

, dest src, , , size of dest - 0, , , , copy().

OpenJDK, , :

public static <T> void copy(List<? super T> dest, List<? extends T> src) {
    int srcSize = src.size();
    if (srcSize > dest.size())
        throw new IndexOutOfBoundsException("Source does not fit in dest");

, , , , ?

+3
4

, . , , .

, . List addAll Collection.

+4

, , . Sun, Collections.copy , List.appendAll() .

, , :

List<IGraphEdge> rgSrc = this._rgGetPath();
List<IGraphEdge> rgDst = new ArrayList<IGraphEdge>(rgSrc.size());
rgDst.addAll(rgSrc);

Duh.

+2

Collection.copy:

. , . , . , .

, , , clone().

+2

Javadocs :

Throws: IndexOutOfBoundsException - , .

, .

+1

Source: https://habr.com/ru/post/1722603/


All Articles