aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Do arrays auto-expand if created using List.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 "Do arrays auto-expand if created using List.toArray()" Watch "Do arrays auto-expand if created using List.toArray()" New topic
Author

Do arrays auto-expand if created using List.toArray()

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

take a look at the following example



The output of the above is



I was expecting an ArrayIndexOutofBounds exception because i declared ia as a fixed array with only 4 elements (i.e. Integer[] ia = new Integer[4];) but the list contains 7 elements. It looks like even though i declared ia as being 4 elements it expanded to 7 elements as shown by the output.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok ill answer my own question. The API says that a new array will be created if it dont fit.


toArray

<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do arrays auto-expand if created using List.toArray()
 
Similar Threads
Why can't we use the IS-A test for the enhanced for-loop?
Using for..each loop on Object[]
Error on K&B book (Java 6) page 579
Unable to understand toArray()
toArray()