Any ideas on why the destination List is not big enough to fit the source? The initial capacity of the destination is scoped to the size of the source. Even if no initial capacity is specified for the destination the runtime still throws the exception but the destination ArrayList should then have a default cap of ten. Twice the number of elements in the source.
Code:import java.util.*; public class Copy{ private List<String> s1list; public Copy(){ s1list = new ArrayList<String>(); s1list.add("Stuff"); s1list.add("me"); s1list.add("im"); s1list.add("a"); s1list.add("List!"); copyElements(s1list); } public void copyElements(List<String> s1list){ List<String> s2list = new ArrayList<String>(s1list.size()); Collections.copy(s2list,s1list); // thorws IndexOutOfBoundsException } public static void main(String[] args){new Copy();} }





Reply With Quote