dbulgakov dbulgakov - 4 months ago 34
Java Question

Cast Object[] array into String[] array RxJava

I am trying to collect some property from list of objects into array using RxJava. I've wrote the code, but now I can't cast return type properly. So, I've tried this code:

.map((range) -> String.format(Locale.getDefault(), "%s (%d)", range, range.getElementNumber()))

But it still returns Object[], and I need String[].


You could use the .toArray() parameter:

.toArray(new String[length]);

It is well described in the documentation:

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. 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.