wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection.toArray() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection.toArray() method" Watch "Collection.toArray() method" New topic
Author

Collection.toArray() method

Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89


The output is: 23 null 33 34 35 78

Can anyone clear my concept why null shows in the output.


"Control time instead of letting time control you."
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18552
    
  40


The output is: 23 null 33 34 35 78

Can anyone clear my concept why null shows in the output.


Here is the relevant excerpt from the JavaDoc -- specifically, the toArray() method of the ArrayList class.


Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.


The second paragraph explains the reason.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
Thanks Henry.
Uttara Rishi
Ranch Hand

Joined: Feb 24, 2008
Posts: 49
Hi,
I still dont understand the output.
Where is the 12 and 23?
Can anybody please explain?

Thanks,
Uttara Rishi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection.toArray() method
 
Similar Threads
Questions about Generics
Generic Confusion
doubt in super keyword in Genrics
doubt abt Generics mock question - from K&B
Study Gude Chap 7 Q16