diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index aae2ce34fe4..5fe2812cd18 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -60,6 +60,38 @@ */ public class ArrayUtils { + /** + * Converts an array of objects to an array of strings, using {@code "null"} to represent {@code null} elements. + * + * @param array the array of objects to convert, may be {@code null} + * @return a string array, {@code null} if input is {@code null} + */ +public static String[] toStringArray(final Object[] array) { + return toStringArray(array, "null"); +} + +/** + * Converts an array of objects to an array of strings, using the provided + * {@code nullReplacement} to represent {@code null} elements. + * + * @param array the array of objects to convert, may be {@code null} + * @param nullReplacement the string to use for {@code null} elements + * @return a string array, {@code null} if input is {@code null} + */ +public static String[] toStringArray(final Object[] array, final String nullReplacement) { + if (array == null) { + return null; + } + final String[] result = new String[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i] == null ? nullReplacement : array[i].toString(); + } + return result; +} + + + + /** * An empty immutable {@code boolean} array. */ @@ -9536,9 +9568,8 @@ public static String toString(final Object array, final String stringIfNull) { * {@code null} if null array input * @since 3.6 */ - public static String[] toStringArray(final Object[] array) { - return toStringArray(array, "null"); - } + /** + /** * Returns an array containing the string representation of each element in the argument @@ -9552,15 +9583,8 @@ public static String[] toStringArray(final Object[] array) { * @return a {@link String} array, {@code null} if null array input * @since 3.6 */ - public static String[] toStringArray(final Object[] array, final String valueForNullElements) { - if (null == array) { - return null; - } - if (array.length == 0) { - return EMPTY_STRING_ARRAY; - } - return map(array, String.class, e -> Objects.toString(e, valueForNullElements)); - } + + /** * ArrayUtils instances should NOT be constructed in standard programming. diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 4163dfbffb4..b1775b537d7 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -90,6 +90,14 @@ void testArraycopyFunction() { assertNullPointerException(() -> ArrayUtils.arraycopy(arr, 0, 0, 1, (Function) null)); } + @Test +public void testToStringArrayHandlesNullElements() { + Object[] input = { "apple", null, 42 }; + String[] result = ArrayUtils.toStringArray(input); + + assertArrayEquals(new String[] { "apple", "null", "42" }, result); +} + @Test void testArraycopySupplier() { final String[] arr = { "a", "b" };