• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic