This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 18506

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)
It is sorta covered in the JavaRanch Style Guide.
subject: Unable to understand toArray()
Similar Threads
Do arrays auto-expand if created using List.toArray()
Using for..each loop on Object[]
Why can't we use the IS-A test for the enhanced for-loop?
Error on K&B book (Java 6) page 579