a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Ben Pittens wrote:After this I have an array str4 with 4 elements "Dog", "Cat", "Monkey", "Elefant".
For me that is strange because I dimensioned it with "new String" as a parameter of the toArray function !
I should excpect that only the "Dog" is in the array because the length of the str4 array is 1.
What use it has then to set a dimension parameter like in new String ?
Does that parameter mention something else?
Ramya Subraamanian wrote:But arr contains only <animal> right . How does this change the value of arr to the contents of the array that is created in toArray
Ramya Subraamanian wrote:The contents of arr3 is <animal> before . but gets modified to the new array that is created in animalList.toArray(arr3). We are creating a new array by passing arr3, as a parameter, why would that modify the content of arr3.
And if you are still unsure, have a look at the output of the ToArray class, especially these two line:
Roel De Nijs wrote:When the array is big enough (as with arr2 and arr3 in the code snippet), there's no need to create another array and the list elements are set to the specified array.
Ben Pittens wrote:Nice to see those examples, now it is clear to me how the list.toArray() method works.
Winston Gutkowski wrote:I would just add however, that you should rarely, if ever, have a need to convert a List into an array, except possibly for legacy code - especially in these halcyon days of version 8.
Roel De Nijs wrote:And "I don't know this method because Winston said I should rarely, if ever, use it" is probably not one of the possible answers