aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unable to understand toArray() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unable to understand toArray() " Watch "Unable to understand toArray() " New topic
Author

Unable to understand toArray()

Maitri Solanki
Greenhorn

Joined: May 11, 2011
Posts: 18

The above program works fine and produces the output as mentioned.
The size of array "ia2" is smaller than the size of the list "iL", then how can the list elements fit into that array?
Why line #1 is not generating compilation error or an exception?

[Edit: added code tags]


Preparing for SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18746
    
  40


Take a look at the JavaDoc for ArrayList again. The toArray(array) method only returns the original array if the elements fit. If it doesn't, it allocates a new array.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Unable to understand toArray()