It's not a secret anymore!
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

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: 20021

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)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Unable to understand toArray()
It's not a secret anymore!