Site icon Automation Dojos

Convert List to Array Using ToArray With Param

There is an overloaded version of the toArray method. This accepts an array as a parameter and returns a result array that is of the same data type as the input array. So basically, this toArray  method can be used to obtain an array that is of the same data type as the input List instead of an object array. The following code demonstrates this approach:

So in this case, the toArray method is invoked with an Integer array that is of the same size as the input List. It then returns an Integer array. So this code prints the same output as before:

2 4 6 8 10
Exit mobile version