In fact input = list.toArray(new String[0])
works without problems, only has a detail.
According to the documentation, the method toArray
checks if the list fits the specified array, and if it doesn’t fit, it allocates another array with the required size:
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
In free translation:
If the list fits in the array, it is returned in this. Otherwise, a new array is allocated, with the same type of the specified array, and the size of this list.
That has a slight implication. If the array is the same size as the list, the elements are copied directly into it, so I don’t even need to pick up the return from toArray
:
ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[list.size()];
// array tem o tamanho da lista, ele é modificado dentro do método
list.toArray(input);
for (String s : input) {
System.out.println(s); // imprime os 3 nomes
}
Notice that I did not assign the return of toArray
for no variable. Since the array is large enough to have all the elements in the list, they are copied directly to the array. This code prints out the 3 names:
NOME 1
NOME 2
NOME 3
But if the array is not large enough, a new array is allocated and returned. The array I passed as argument to toArray
is not modified:
ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[0];
list.toArray(input);
for (String s : input) {
System.out.println(s);
}
In this case, the array input
not large enough to contain all the elements on the list, so toArray
allocate another array with the proper size and returns it. But I did not assign this return in any variable, and the array input
remains empty. That is why this code does not print anything.
In order for the above code to work properly, I would have to do input = list.toArray(input)
.
Finally, if the array is larger than the list, the excess elements will be null:
ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[list.size() + 10];
list.toArray(input);
for (String s : input) {
System.out.println(s);
}
In this case, the array is larger than the list, and the output is:
NOME 1
NOME 2
NOME 3
null
null
null
null
null
null
null
null
null
null
And in this case, even if I do input = list.toArray(input)
, the output is also the same. After all, this is what is described in the documentation: if the list fits in the array, the elements are copied directly to this one. Then in this case the returned array is the same that was passed to toArray
, since it has plenty of space to contain all the elements of the list.
We can also do another test, to confirm whether another array is allocated or not, taking the return of toArray
and comparing with the original array, to know if in fact another array is being returned or the same:
ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[0];
String[] other = list.toArray(input);
System.out.println(other == input);
In this case, the code prints false
, as well input
not large enough to have all the elements on the list, toArray
allocates another array (which is returned and assigned in the variable other
).
But if the size of input
for list.size()
(or any value greater than that), then the code prints true
, because now the array has space for all elements of the list, no new array is allocated.
In short, if you make any of the alternatives below:
String[] input = list.toArray(new String[0]);
String[] input = list.toArray(new String[list.size()]);
Both work. The difference is that the first will create 2 arrays: one with zero size, and the other that is allocated internally by toArray
, since the zero-size array has no space for the list elements (except if the list is empty, because it returns the same array). The second alternative will only allocate an array and return it.
So in this way, all values that are contained in Arraylist<String> passes to String[] right?
– André alas
@Even so, all values of your List (Arraylist<String>) go to the array (String[])
– Murilo Portugal
Thank you very much.... Thanks so much for the help!!
– André alas
@But if you do
list.toArray(new String[0])
also works, see. In this case, if you pass an array with zero size, internalize the methodtoArray
allocates another with the required size as described in documentation– hkotsubo
@Anyway, I put an answer detailing this behavior
– hkotsubo