File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unable to understand toArray() " Watch "Unable to understand toArray() " New topic

Unable to understand toArray()

Maitri Solanki

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

Joined: Sep 28, 2004
Posts: 20538

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Unable to understand toArray()
It's not a secret anymore!