|
-
Jun 23rd, 2005, 08:39 PM
#1
Thread Starter
Dazed Member
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();}
}
Last edited by Dilenger4; Jun 26th, 2005 at 09:07 PM.
Reason: Resolved
-
Jun 24th, 2005, 06:40 AM
#2
Re: Copying Collections?
The constructor only sets the capacity of the target list, not its size. You need the size to be large enough.
Use the addAll method every collection has instead of Collections.copy. Or pass s1List directly to the constructor of ArrayList.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 24th, 2005, 08:06 AM
#3
Thread Starter
Dazed Member
Re: Copying Collections?
If i create an ArrayList with a capacity of say ten. That's the maximum amount that the ArrayList can hold. I can't see how trying copying five elements into a data structure thats specified to hold a max of ten does't work.
I thought size was the amount of elements that could be contained within but the size method in ArrayList returns the amount of elements contained in the ArrayList not the size or capacity.
-
Jun 24th, 2005, 08:27 AM
#4
Re: Copying Collections?
If i create an ArrayList with a capacity of say ten. That's the maximum amount that the ArrayList can hold.
Actually, not quite.
An ArrayList distinguishes between capacity and size. Capacity is the maximum size the list can grow to without having to reallocate. Size is the count of elements the list holds right now. The memory that is allocated but not included in size is not considered part of the list. The list can hold any amount of elements by appending them, thus increasing the size. However, you cannot copy to that area because it's not yet part of the list. You have to use the add operations. Copying is only to put values where there already is something.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 24th, 2005, 08:59 AM
#5
Thread Starter
Dazed Member
Re: Copying Collections?
Well that just bites.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|