When using javax.mail.Message#setRecipeints
, I noticed a thing for the first time and wanted to know more about it.
This is my code:
List<InternetAddress> ccRecipients = new ArrayList<InternetAddress>();
// have a valid and working
// method that fills the list with the data (InternetAddress objects)
message.setRecipients(
RecipientType.CC, ccRecipients.toArray(new InternetAddress[0]));
Here I am not getting why do we require to create a new InternetAddress
array and pass the element at 0th index in the toArray
method?
Can anyone explain why do we require this to be done?
I think it has something to do with this special toArray
method.
I read the following from the API docs but didn't get the last part (in bold) :
Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
How does it save allocation costs?
toArray()
without parameter returns type of Object
but toArray(T[] a)
returns type of T[]
. This is a generic method of List.