Java Array to String toString(Object array)

Here you can find the source of toString(Object array)

Description

Constructs a string representation for the given array that describes its contents (as opposed to the default Object#toString() version that arrays inherit).

License

Apache License

Parameter

Parameter Description
array an array

Exception

Parameter Description
IllegalArgumentException if array is not actually an array

Return

a string representation for the given array that describes its contents

Declaration

public static String toString(Object array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    /**//w  w  w . j  ava 2  s.com
     * Constructs a string representation for the given array that describes its contents (as
     * opposed to the default {@link Object#toString()} version that arrays inherit). This provides a
     * convenience above and beyond similar methods in {@link Arrays} because it accepts any array
     * type, including primitive array types.
     *
     * @param array an array
     * @return a string representation for the given array that describes its contents
     * @throws IllegalArgumentException if {@code array} is not actually an array
     * 
     * @see Arrays#toString(Object[])
     */
    public static String toString(Object array) {
        Class<?> arrayType = array.getClass();
        if (!arrayType.isArray()) {
            throw new IllegalArgumentException("specified object is not an array");
        }
        Class<?> componentType = arrayType.getComponentType();
        if (componentType.isPrimitive()) {
            if (componentType == boolean.class) {
                return Arrays.toString((boolean[]) array);
            } else if (componentType == byte.class) {
                return Arrays.toString((byte[]) array);
            } else if (componentType == char.class) {
                return Arrays.toString((char[]) array);
            } else if (componentType == short.class) {
                return Arrays.toString((short[]) array);
            } else if (componentType == int.class) {
                return Arrays.toString((int[]) array);
            } else if (componentType == long.class) {
                return Arrays.toString((long[]) array);
            } else if (componentType == float.class) {
                return Arrays.toString((float[]) array);
            } else if (componentType == double.class) {
                return Arrays.toString((double[]) array);
            } else {
                throw new AssertionError("Unrecognized primitive type: " + componentType);
            }
        } else {
            return Arrays.toString((Object[]) array);
        }
    }
}

Related

  1. toString(int[] codes)
  2. toString(int[] v, char delim)
  3. toString(long[] dom)
  4. toString(Map arg)
  5. toString(Object array)
  6. toString(Object array)
  7. toString(Object array[])
  8. toString(Object[] a)
  9. toString(Object[] array)