File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed in Generics toArray() method

 
peter kosmas
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • 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!
))
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mumtaz Khan
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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


 
Bob Wheeler
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic