Java Array to String arrayToString(final Object[] array)

Here you can find the source of arrayToString(final Object[] array)

Description

Provides a string representation of the provided array of objects.

License

Apache License

Parameter

Parameter Description
array the array of objects to represent

Return

a string representation of the objects

Declaration

public static final String arrayToString(final Object[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Tiese Barrell/*from w  w w. ja  va 2  s. com*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    private static final String NULL_VALUE = "<null>";
    private static final String ARRAY_START = "{";
    private static final String ARRAY_END = "}";
    private static final String ARRAY_SEPARATOR = ", ";

    /**
     * Provides a string representation of the provided array of objects.
     * 
     * @param array
     *            the array of objects to represent
     * @return a string representation of the objects
     */
    public static final String arrayToString(final Object[] array) {
        final StringBuilder builder = new StringBuilder();
        builder.append(ARRAY_START);
        if (array != null && array.length > 0) {
            appendNonEmptyArrayToString(array, builder);
        }
        builder.append(ARRAY_END);
        return builder.toString();
    }

    private static void appendNonEmptyArrayToString(final Object[] array, final StringBuilder builder) {
        for (int i = 0; i < array.length; i++) {
            final String toAppend = array[i] == null ? NULL_VALUE : array[i].toString();
            builder.append(toAppend);
            if (i != array.length - 1) {
                builder.append(ARRAY_SEPARATOR);
            }
        }
    }
}

Related

  1. arrayToString(final byte[] array)
  2. arrayToString(final byte[] array, final int start, final int length)
  3. arrayToString(final float[] array)
  4. arrayToString(final Object[] a)
  5. arrayToString(final Object[] arr)
  6. arrayToString(final Object[] array)
  7. arrayToString(final String[] args)
  8. arrayToString(final String[] strs)
  9. arrayToString(final String[] value)