This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 16695
    
  19


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)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Unable to understand toArray()
 
Similar Threads
toArray()
Why can't we use the IS-A test for the enhanced for-loop?
Do arrays auto-expand if created using List.toArray()
Using for..each loop on Object[]
Error on K&B book (Java 6) page 579