Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Help needed in Generics toArray() method

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
))
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
peter kosmas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic