File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help needed in Generics toArray() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help needed in Generics toArray() method" Watch "Help needed in Generics toArray() method" New topic
Author

Help needed in Generics toArray() method

peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
Hello i was doing some experiments with code so i can answer some difficult questions in Generics
and i can't understand what the parameter in Collections toArray(T[] a) means
this example of code that i was trying
List<String> list=new Vector<String>();
String[] a=list.toArray(new String[0]);
this works too if i type
String[] a=list.toArray(new String[]{"","",""});
now the question is ! what the Hell is this parameter for?
Can't understand it
some help would be very Handy!
))

Time is relative so there is no way i can be late.
~Albert Einstein~
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi Peter,

declaration of this method is:



It means that when parameter 'a' is of type T, then function has to return array of objects of type T.
This parameter only tells function what type should have objects in the returned array.
When you pass as argument array of type String, then function will return array of type String,
when you pass array of type Object, then function will return array of Objects.
Content of this array passed to function is simply ignored, because toArray returns objects from collection.

Hope this helps.
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
Here is another code snippet, Output of running this code is
s size 2
s1 s1 size 3
1
2
3
.
If you change s and s1 to Integer[], it will give java.lang.ArrayStoreException which is beacause runtime type of the specified array is not a supertype of the runtime type of every element in this list.


SCJP 5.0 -- 97%
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79

ok
That was helpfull
Thanks a lot ! This is a bit complicated !
And found a lot of those questions on exams of Devaka and whizlabs
i suppose i will encounter some too on the Exam


Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Sorry guys,

but the parameter of the toArray method are not lost. Look here:


Output:

So it doesn't overwrite the list members, but appending it does, if the list size is smaller than the T[] array !?
And what happens at index 3? Is the reason the difference between the definition of position and index?

Thanks guys for helping me out here.
cheers Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Bob Wheeler wrote:Sorry guys,

but the parameter of the toArray method are not lost. Look here:


Output:

So it doesn't overwrite the list members, but appending it does, if the list size is smaller than the T[] array !?
And what happens at index 3? Is the reason the difference between the definition of position and index?

Thanks guys for helping me out here.
cheers Bob

Bob,

The answers are in the API documentation. If the array is big enough to hold all the elements in the list, then the list elements are copied to the array, starting at index 0. If the array is bigger than the list, then the first element which doesn't belong to the list is set to null to act as a sentinel value. If the array is not big enough to hold the elements in the list, then a new array object is instantiated instead.


All code in my posts, unless a source is explicitly mentioned, is my own.
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Ruben Soto wrote:
The answers are in the API documentation. If the array is big enough to hold all the elements in the list, then the list elements are copied to the array, starting at index 0. If the array is bigger than the list, then the first element which doesn't belong to the list is set to null to act as a sentinel value. If the array is not big enough to hold the elements in the list, then a new array object is instantiated instead.

Ruben, forgot to thank you for your answer. So here you go: Thank you
Bob
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Bob Wheeler wrote:
Ruben Soto wrote:
The answers are in the API documentation. If the array is big enough to hold all the elements in the list, then the list elements are copied to the array, starting at index 0. If the array is bigger than the list, then the first element which doesn't belong to the list is set to null to act as a sentinel value. If the array is not big enough to hold the elements in the list, then a new array object is instantiated instead.

Ruben, forgot to thank you for your answer. So here you go: Thank you
Bob

You're welcome, Bob. The important thing is that you benefitted from the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed in Generics toArray() method