| Author |
Collections copy error
|
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
I get a compilation error -> The method copy(List<? super T>, List<? extends T>) in the type Collections is not applicable for the arguments (List<String>, List<Object>)
what does List<? super T> & List<? extends T> signifies... i am simply copying from List<String> to List<Object> which is in my terms a more distinct hierarchy to the most generic Object list which Java should allow?
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
My mistake.. it should have been Collections.copy(copyList,testList);
Sorry!
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
I modified the code but get IndexOutOfBoundsException: Source does not fit in dest
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Have you read the Javadocs for that method? They explain why it won't work.
However, you should be able to get the effect you want like this:
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Thanks it worked. can you tell me why second one is throwing indexoutofbounds exception even if i declare list of big size?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You declared it with a big capacity (the number of elements it can hold before its internal structure needs to change). The size (the actual number of elements) is still 0. You'll first need to add several objects before the size increases.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
That's capacity, not size. The target list actually has to contain that many elements.
I'm not entirely sure why the method is written like that - it seems a bit pointless - but it's how it's defined.
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Mathew & Rob - thanks!
it worked after doing below
|
 |
 |
|
|
subject: Collections copy error
|
|
|