Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/main/java/org/apache/commons/lang3/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ void testArraycopyFunction() {
assertNullPointerException(() -> ArrayUtils.arraycopy(arr, 0, 0, 1, (Function<Integer, String[]>) 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" };
Expand Down
Loading