Results 1 to 5 of 5

Thread: Copying Collections?

Threaded View

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved Copying Collections?

    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();}
     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width