This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to understand toArray()

 
Maitri Solanki
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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]
 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic