The output of the code snippet
List<Integer> list = List.of(10, 20, 30, 40, 50);
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
list.toArray(array);
System.out.println(Arrays.toString(array));
is:
[10, 20, 30, 40, 50, null, 7, 8, 9, 0]
I have found following code in ArrayList.toArray method:
if (a.length > size)
a[size] = null;
What is the reason for such behavior?
Why the 5th element in may example is null, but not 6?