Java Array to String toString(Object array)

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

Description

to String

Declaration

public static String toString(Object array) 

Method Source Code

//package com.java2s;

import java.util.Arrays;

public class Main {
    public static String toString(Object array) {
        if (array == null) {
            return null;
        }//from   w  ww. j  a  v  a  2s  .  c  o  m

        Class<?> type = array.getClass();
        if (type.isArray()) {
            if (type.getComponentType().isPrimitive()) {
                if (type == byte[].class) {
                    byte[] valueArray = (byte[]) array;
                    // using default encoding
                    return new String(valueArray, 0, valueArray.length);
                }
                if (type == char[].class) {
                    char[] charArray = (char[]) array;
                    return new String(charArray);
                }
                if (type == short[].class) {
                    return Arrays.toString((short[]) array);
                }
                if (type == int[].class) {
                    return Arrays.toString((int[]) array);
                }
                if (type == long[].class) {
                    return Arrays.toString((long[]) array);
                }
                if (type == float[].class) {
                    return Arrays.toString((float[]) array);
                }
                if (type == double[].class) {
                    return Arrays.toString((double[]) array);
                }
                if (type == boolean[].class) {
                    return Arrays.toString((boolean[]) array);
                }
            } else {
                return Arrays.toString((Object[]) array);
            }
        }
        return array.toString();
    }
}

Related

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