Τετάρτη, 16 Ιουλίου 2008

Collections.copy

Well, it looks like this method contains a "bug"?!?!?

Simply do this test:

public void testCollectionsCopy () {
ArrayList list1 = new ArrayList(3);
list1.add("one");
list1.add("two");
list1.add("three");
ArrayList list2 = new ArrayList(3);
Collections.copy(list2, list1); // throws IndexOutOfBoundsException!!!!!
}
Well, the solution? Pass the collection to be copied as a parameter to the constructor.
ArrayList list1 = new ArrayList(list1);