tags:

views:

175

answers:

4
+1  A: 

I wasn't the first to run into this, but it really is always your fault. According to the response to this bug report at Sun, Collections.copy is not intended to be used like this, you should use List.appendAll() instead.

So I fixed my code to look like this and it worked:

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

Duh.

Hanno Fietz
Better would be: `List<IGraphEdge> rgDst = new ArrayList<IGraphEdge>(rgSrc)`
erickson
Right, that's even better, thanks.
Hanno Fietz
+3  A: 

It's not looking at the capacity, it's looking at the size. That is, the destination should contain elements, which are to be overwritten by the source list.

I've never run into a case where this would be useful. Usually what you want is a copy constructor in one of the List implementations, or the addAll method of the Collection interface.

erickson
I took "must be at least as long" to mean "must have at least the capacity to contain all items".
Hanno Fietz
+1  A: 

From the documentation for Collection.copy:

Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.

I agree with you that it's not terribly intuitive, but it seems like what you're really trying to do is more like clone().

Steve B.
I took "must be at least as long" to mean "must have at least the same capacity", which isn't what it *did* mean. `clone()`, as long as it's shallow, is what I want here, but for lists specifically, I can in fact use `addAll` or the *constructor of ArrayList* (duh).
Hanno Fietz
+1  A: 

The Javadocs say:

Throws: IndexOutOfBoundsException - if the destination list is too small to contain the entire source List.

So it seems to be conforming to the spec.

z5h